Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling columns of record by it´s index, not name

Tags:

record

plsql

I have table with more than 160 columns and I need to work with each column in PL/SQL procedure. I make a record of whole row from the table like this:

DECLARE
  l_employee rec_employees%ROWTYPE;
  COLUMN_AND_VALUE VARCHAR2(200);

BEGIN
  SELECT *
  INTO l_employee
  FROM employees
  WHERE employee_id = 100;

and now I want to work with all columns of this record with FOR LOOP, but I don't know how, because I have to refer to each column of record by its column name like l_employee.id, l_employee.salary,.... is it possible to refer to them in order somehow like l_employee[INDEX_OF_COLUMN] and also get the name of column I am working with? Here is the example I want to do with record:

FOR INDEX_OF_COLUMN IN 1 .. 167 LOOP
     COLUMN_AND_VALUE := l_employee[INDEX_OF_COLUMN].COLUMN_NAME || ': ' || l_employee[INDEX_OF_COLUMN].VALUE_OF_COLUMN

-- I know those commands don't work, but I need something like that

END LOOP;

Or is here better way to do it without using record?

Thank you very much and I'm really sorry for my bad English. I hope you understood my question :)

like image 531
Mino1234 Avatar asked Sep 15 '25 15:09

Mino1234


1 Answers

Finally i found with some help this solution. It is exactly what i needed in this post, but then i realised it´s not the best way for me because of another conditions i had.

`DECLARE  
  l_query      VARCHAR2(32767) := 'SELECT * FROM employees where id=1';  
  l_theCursor  INTEGER DEFAULT dbms_sql.open_cursor;  
  l_columnValue VARCHAR2(4000);  
  l_status      INTEGER;  
  l_descTbl dbms_sql.desc_tab;  
  l_colCnt NUMBER;  
BEGIN  
  dbms_sql.parse(l_theCursor,l_query,dbms_sql.native);  
  dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl);  
  FOR i IN 1 .. l_colCnt  
  LOOP  
    dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);  
  END LOOP;  
  l_status := dbms_sql.execute(l_theCursor);  
  WHILE ( dbms_sql.fetch_rows(l_theCursor) > 0 )  
  LOOP  
    FOR i IN 1 .. l_colCnt  
    LOOP  
      dbms_sql.column_value( l_theCursor, i, l_columnValue );  
      dbms_output.put_line( l_descTbl(i).col_name|| ': ' ||l_columnValue ); 
    END LOOP;
  END LOOP;  
EXCEPTION  
WHEN OTHERS THEN  
  dbms_sql.close_cursor( l_theCursor );  
  RAISE;  
END;  `
like image 108
Mino1234 Avatar answered Sep 18 '25 10:09

Mino1234