Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Referring to Cursor Row and Column in Dynamic SQL

743768Oct 25 2010 — edited Oct 26 2010
I have a procedure that dynamically reads a schema name and table name from an input table. The code then needs to loop through all rows and columns of each table and output the data. I'm 95% done with what I want to accomplish, but there is one small bug. The line dbms_output.put(*col.column_name* || '',''); ' ||
should refer to something like rec.col.column_name so that it gets the column of the current record. Right now it just displays the column name for each record instead of the actual value. Can anyone help me tweak the code to get the actual value?

CREATE OR REPLACE PACKAGE BODY some_proc IS
-- Function and procedure implementations
PROCEDURE create_files IS
CURSOR c_tbls IS
SELECT * FROM tbl_list;
l_sql VARCHAR2(4000);
BEGIN
--Loop through all tables
FOR tbl IN c_tbls LOOP
l_sql := 'DECLARE ' || ' CURSOR c_tbl_recs IS ' || ' SELECT * ' ||
' FROM ' || tbl.schema_name || '.' || tbl.table_name || '; ' ||
' t_tbl_rowtype c_tbl_recs%ROWTYPE; ' || 'BEGIN ' ||
' FOR rec IN c_tbl_recs LOOP ' ||
' FOR col IN (SELECT column_name ' ||
' FROM dba_tab_cols ' ||
' WHERE owner = ''' || tbl.schema_name || '''' ||
' AND table_name = ''' || tbl.table_name || '''' ||
' ORDER BY column_id) LOOP ' ||
*' dbms_output.put(col.column_name || '',''); ' ||* ' END LOOP; dbms_output.put_line(''''); END LOOP; ' ||
'END; ';
--dbms_output.put_line(l_sql);
EXECUTE IMMEDIATE l_sql;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END;
END;
This post has been answered by hm on Oct 25 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 23 2010
Added on Oct 25 2010
4 comments
473 views