Hi all,
I am having a little problem with ref cursors that I hope someone can help me with.
I am trying to use them to generically audit any table on the system.
That means, the procedure will never know what the table structure or columns involved are until it is called.
This is my code portions with notes as to what I am trying to do with the problem noted below.
I tried declaring a rowtype on the ref cursor, as below, but sql didn't like it:
I know the syntax is supposed to be something like:
but I want a generic procedure that will not need EMPLOYEES hardcoded.
I need to either be able to use a dynamic %ROWTYPE with my ref cursor or find some way of modifying my the INTO portion of my fetch statement so it can cope with any number of fields.
Any help will be much appreciated!
I am having a little problem with ref cursors that I hope someone can help me with.
I am trying to use them to generically audit any table on the system.
That means, the procedure will never know what the table structure or columns involved are until it is called.
This is my code portions with notes as to what I am trying to do with the problem noted below.
Code:
type field_type is table of varchar2(32)
index by binary_integer;
audit_field field_type;
idx binary_integer := 0;
iterator binary_integer := 0;
type rc is REF CURSOR;
select_field_curs rc;
...
OPEN data_child_curs;
idx := 0;
-- This builds up an array of column names that I would like to audit.
LOOP
fetch data_child_curs into data_child_rec;
exit when data_child_curs%NOTFOUND;
idx := idx + 1;
audit_field(idx) := data_child_rec.audited_field;
END LOOP;
--This value is the number of fields that will be audited.
no_of_fields := idx;
col := ' ';
idx := 1;
-- This loop builds up a string of all of the columns I would like to select
-- ie if 3 fields, say 'FIELD1, FIELD2, FIELD3'
LOOP
EXIT when idx > no_of_fields;
t_field := audit_field(idx);
IF idx = 1 THEN
col := t_field;
ELSE
col := col || ', ' || t_field;
END IF;
idx := idx + 1;
END LOOP;
-- This is where I use this newly built up list of column names to construct my select statement
open select_field_curs for
'SELECT ' ||
col ||
' FROM ' ||
t_tablename ||
' WHERE projectid = ' ||
p_projectid;
-- Now is where I have my problem.
-- I need to select the columns into fields, so that I can use them.
-- This example shows how much code there is for 4 fields, there can a lot more potentially.
IF no_of_fields = 1
THEN
fetch select_field_curs
INTO t_field_1;
EXIT when select_field_curs%NOTFOUND;
ELSE
IF no_of_fields = 2
THEN
fetch select_field_curs
INTO t_field_1, t_field_2;
EXIT when select_field_curs%NOTFOUND;
ELSE
IF no_of_fields = 3
THEN
fetch select_field_curs
INTO t_field_1, t_field_2, t_field_3;
EXIT when select_field_curs%NOTFOUND;
ELSE
IF no_of_fields = 4
THEN
fetch select_field_curs
INTO t_field_1, t_field_2, t_field_3, t_field_4;
EXIT when select_field_curs%NOTFOUND;
END IF;
END IF;
END IF;
END IF;
I tried declaring a rowtype on the ref cursor, as below, but sql didn't like it:
Code:
type rc is REF CURSOR;
select_field_curs rc;
select_field_rec select_field_curs%ROWTYPE;
Code:
type rc is ref cursor return EMPLOYEES%rowtype;
I need to either be able to use a dynamic %ROWTYPE with my ref cursor or find some way of modifying my the INTO portion of my fetch statement so it can cope with any number of fields.
Any help will be much appreciated!