I need to create a trigger in Oracle 11g for auditing a table. I have a table with 16 columns that need to be audited.
For every new insert into a table, I need to put an entry in audit table for each column inserted i.e. in this scenario 16 row will be inserted in audit table.
For every update, suppose I update 1st and 2nd column, then it will create two record in audit with its old value and new value. Structure of audit table will be:
id
mod_col_name
OLD VALUE
NEW VALUE
upd_time
mod_user_id
My approach:
create or replace trigger my_trigger
after update or insert on temp12
for each row
declare
TYPE tab_col_nt IS table of varchar2(30);
v_tab_col_nt tab_col_nt;
begin
v_tab_col_nt := tab_col_nt('id','name','salary'); --in example i have given only 3 column name
for r in v_tab_col_nt.first..v_tab_col_nt.last
loop
if updating(v_tab_col_nt(r)) then
insert into audit_table values (
id_seq.nextval, v_tab_col_nt(r), :old.v_tab_col_nt(r),
:new.v_tab_col_nt(r), sysdate, user
); --here :old & :new syntex is not working
end if;
if inserting then
insert into audit_table values (
id_seq.nextval, v_tab_col_nt(r), null,
:new.v_tab_col_nt(r), sysdate, user);
end if;
end loop;
end;
My concern:
here :old.v_tab_col_nt(r), :new.v_tab_col_nt(r) is not working
how to track user id through which user is logged in GUI (dot net is front end).
I want this trigger to be written dynamically.