Dynamic SQL question for triggers
All,
I was wondering if it's possible to use the :new.<column_name>
and :old.<column_name> calls inside of a dynamic sql call inside
of a trigger? I am getting an error when trying this. Is there
any other way to accomplish this?
In summary, I'm trying to write a generic audit trigger that
will audit all columns of any table, with only supplying a table
name, regardless of datamodel changes to that table. Any ideas
would be greatly appreciated. Trigger code and error message
are both below. Thanks,
John
Trigger Code:
CREATE OR REPLACE TRIGGER JM_TEST_tr3
AFTER INSERT OR UPDATE
ON JM_TEST
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
declare
vcTableName VARCHAR2(40) := 'JM_TEST';
cursor c1 is
Select ':new.' || column_name column_name, data_type
from user_tab_columns
where table_name = vcTableName;
Begin
if INSERTING then
for c1_rec in c1 loop
execute immediate 'insert into jm_audit_test
( table_name, action_name, column_name, new_value,
old_value )
values
( '''|| vcTableName || ''', ''INSERT'', ''' ||
c1_rec.column_name || ''', :new.' || c1_rec.column_name || ',
null )';
end loop;
end if;
end;
Error msg when I try to insert a row:
ORA-00600: internal error code, arguments: [15212], [1], [], [],
[], [], [], []
ORA-06512: at "CTS2000.JM_TEST_TR3", line 12
ORA-04088: error during execution of
trigger 'CTS2000.JM_TEST_TR3'