Hi Guys,

I have a situation where I need to access the column values from rowtype variable. However, the column names are dynamic.

below is sample code:
declare

Cursor c1 is select * from emp;
Cursor c2 is select column_name from xyztable;
v_c2 c2%rowtype;
v_str varchar2
v_value varchar2(200);

begin

for rec in c1
loop
open c2;---this cursor has column names like EMPLOYEE_ID, FIRST_NAME, LAST_NAME etc.
loop
fetch c2 into v_c2;
exit when c2%notfound;

/* now lets say i want to access value of LAST_NAME from cursor c1, so I am writing below code, however it does not work as expected */

v_str:= 'rec.'|| v_c2.column_name; -- this will give me string like "rec.EMPLOYEE_ID"

v_value:=v_str;



end loop;

end loop;



end;
/

Plz help ASAP.Thanks.

Answer Posted / ajitnayak

DECLARE
CURSOR C1 IS SELECT *
FROM DEPT;

CURSOR C2 IS SELECT *
FROM EMP;

v_c2 C2%ROWTYPE;
v_str VARCHAR2(30);
v_res VARCHAR2(35);

BEGIN
OPEN C2;

FOR REC IN C1
LOOP
FETCH C2 INTO v_c2;
EXIT WHEN C2%NOTFOUND;
v_str := 'REC'||v_c2.ENAME;
v_res := V_STR;
DBMS_OUTPUT.PUT_LINE('NAME'||v_c2.ENAME);
END LOOP;
CLOSE C2;
END;

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is the difference between a having clause and a where clause? : Sql dba

494


Is left join faster than inner join?

570


What are the advantages of sql? Explain

615


What is rollback?

556


how to select first 5 records from a table? : Sql dba

488






What is difference between rank () row_number () and dense_rank () in sql?

539


What is db journal file?

493


What is varchar sql?

583


What is pl sql in dbms?

486


How many postgresql users are there, worldwide?

541


Is sql workbench free?

502


Can you have more than one key in a database?

484


What is the difference between rollback and rollback to statements?

558


What is the use of function in sql?

511


Explain aggregate functions are available there in sql?

510