I checked out DBMS_SQL.DESCRIBE and I was able to get the column names. But how do I get the values as well? Basically I need Column Name/value pairs for each record.
I'll check out SYS_CONTEXT
thx
Main Topics
Browse All TopicsWhile looping through a cursor I need to loop through each column without knowing the column name. For instance
This works:
LOOP
FETCH MVAR_cursor into MVAR_record;
EXIT WHEN MVAR_cursor%NOTFOUND;
MVAR_record.COLUMNNAME1;
MVAR_record.COLUMNNAME2;
END LOOP;
CLOSE MVAR_cursor;
Since I won't know the column name, I need something like this:
LOOP
FETCH MVAR_cursor into MVAR_record;
EXIT WHEN MVAR_cursor%NOTFOUND;
i := i+1;
MVAR_record.column(i);
END LOOP;
CLOSE MVAR_cursor;
thx
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
See this code:
SQL> select * from tab1;
EMPNO ENAME JOB
---------- ---------- ---------
7369 SMITH CLERK
7499 ALLEN SALESMAN
declare
l_cur number;
l_stmt varchar2(2000);
l_dtbl dbms_sql.desc_tab;
l_cnt number;
l_status number;
l_val varchar2(200);
begin
l_cur := dbms_sql.open_cursor;
l_stmt := 'select * from tab1';
dbms_sql.parse(l_cur,'sele
dbms_sql.describe_columns(
for i in 1..l_cnt loop
dbms_sql.define_column(l_c
end loop;
l_status := dbms_sql.execute(l_cur);
while ( dbms_sql.fetch_rows(l_cur)
for i in 1..l_cnt loop
dbms_sql.column_value(l_cu
dbms_output.put_line(l_dtb
end loop;
end loop;
dbms_sql.close_cursor(l_cu
end;
/
declare
stmt varchar2(2000) :=null;
begin
for col_names in (select COLUMN_NAME from all_tab_columns where table_Name = upper('TABLE_NAME') and owner = 'SCHEMA_NAME' and column_name like '%SOMETHING%' order by COLUMN_NAME)
loop
stmt := stmt||','||col_names.COLUM
end loop;
insert into temp select substr(stmt,2,length(stmt)
commit;
end;
Business Accounts
Answer for Membership
by: SJT2003APosted on 2007-06-11 at 13:19:42ID: 19261452
As a quick one I would suggest to check the DBMS_SQL package provided by ORACLE.
type = ' maxlen = ' name = ' name_len = ' schema_nam e = ' schema_nam e_len = ' precision = ' scale = ' ok = '); '); e');
c, col_cnt, rec_tab);
);
May be the DBMS_SQL package would be helpful to achieve your goal.
It offers DESCRIBE functionality list out the columns stored in a REF CURSOR and enables you to program accordingly.
Check the following example (extract from OTN):
This can be used as a substitute to the SQL*Plus DESCRIBE call by using a SELECT * query on the table that you want to describe.
declare
c number;
d number;
col_cnt integer;
f boolean;
rec_tab dbms_sql.desc_tab;
col_num number;
procedure print_rec(rec in dbms_sql.desc_rec) is
begin
dbms_output.new_line;
dbms_output.put_line('col_
|| rec.col_type);
dbms_output.put_line('col_
|| rec.col_max_len);
dbms_output.put_line('col_
|| rec.col_name);
dbms_output.put_line('col_
|| rec.col_name_len);
dbms_output.put_line('col_
|| rec.col_schema_name);
dbms_output.put_line('col_
|| rec.col_schema_name_len);
dbms_output.put_line('col_
|| rec.col_precision);
dbms_output.put_line('col_
|| rec.col_scale);
dbms_output.put('col_null_
if (rec.col_null_ok) then
dbms_output.put_line('true
else
dbms_output.put_line('fals
end if;
end;
begin
c := dbms_sql.open_cursor;
dbms_sql.parse(c, 'select * from scott.bonus', dbms_sql);
d := dbms_sql.execute(c);
dbms_sql.describe_columns(
/*
* Following loop could simply be for j in 1..col_cnt loop.
* Here we are simply illustrating some of the PL/SQL table
* features.
*/
col_num := rec_tab.first;
if (col_num is not null) then
loop
print_rec(rec_tab(col_num)
col_num := rec_tab.next(col_num);
exit when (col_num is null);
end loop;
end if;
dbms_sql.close_cursor(c);
end;
/
And another option is using SYS_CONTEXT storing context/variables and retrieve later. Check it out.
Good luck :)