You know what -- if you don't know what the code is doing or how it does it (and cannot figure it out) -- it might be best if you didn't use it.
You did not follow the instructions above (else you would have :new.rowid not 'rowid' in the insert statement). I don't know what else you did wrong. Just to put it to bed, here you go. Just CUT AND PASTE this. Try to understand what it does and how it does it.
create table audit_tbl
( timestamp date,
who varchar2(30),
tname varchar2(30),
cname varchar2(30),
old varchar2(2000),
new varchar2(2000),
rid rowid
)
/
create or replace package audit_pkg
as
procedure check_val( l_tname in varchar2,
l_cname in varchar2,
l_new in varchar2,
l_old in varchar2,
l_rowid in varchar2 );
procedure check_val( l_tname in varchar2,
l_cname in varchar2,
l_new in date,
l_old in date,
l_rowid in varchar2 );
procedure check_val( l_tname in varchar2,
l_cname in varchar2,
l_new in number,
l_old in number,
l_rowid in varchar2 );
end;
/
create or replace package body audit_pkg
as
procedure check_val( l_tname in varchar2,
l_cname in varchar2,
l_new in varchar2,
l_old in varchar2,
l_rowid in varchar2 )
is
begin
if ( l_new <> l_old or
(l_new is null and l_old is not NULL) or
(l_new is not null and l_old is NULL) )
then
insert into audit_tbl values
( sysdate, user, upper(l_tname), upper(l_cname),
l_old, l_new, l_rowid );
end if;
end;
procedure check_val( l_tname in varchar2, l_cname in varchar2,
l_new in date, l_old in date,
l_rowid in varchar2 )
is
begin
if ( l_new <> l_old or
(l_new is null and l_old is not NULL) or
(l_new is not null and l_old is NULL) )
then
insert into audit_tbl values
( sysdate, user, upper(l_tname), upper(l_cname),
to_char( l_old, 'dd-mon-yyyy hh24:mi:ss' ),
to_char( l_new, 'dd-mon-yyyy hh23:mi:ss' ), l_rowid );
end if;
end;
procedure check_val( l_tname in varchar2, l_cname in varchar2,
l_new in number, l_old in number,
l_rowid in varchar2 )
is
begin
if ( l_new <> l_old or
(l_new is null and l_old is not NULL) or
(l_new is not null and l_old is NULL) )
then
insert into audit_tbl values
( sysdate, user, upper(l_tname), upper(l_cname),
l_old, l_new, l_rowid );
end if;
end;
end audit_pkg;
/
And the script to generate the trigger would be:
set serveroutput on
set feedback off
set verify off
set embedded on
set heading off
spool tmp.sql
prompt create or replace trigger aud#&1
prompt after update on &1
prompt for each row
prompt begin
select ' audit_pkg.check_val( ''&1'', ''' || column_name ||
''', ' || ':new.' || column_name || ', :old.' ||
column_name || ', :new.rowid);'
from user_tab_columns where table_name = upper('&1')
/
prompt end;;
prompt /
spool off
set feedback on
set embedded off
set heading on
set verify on
@tmp
The test I used is:
ops$tkyte@ORA817DEV.US.ORACLE.COM> create table dept as select * from scott.dept where rownum = 1;
Table created.
ops$tkyte@ORA817DEV.US.ORACLE.COM> @test dept
create or replace trigger aud#dept
after update on dept
for each row
begin
audit_pkg.check_val( 'dept', 'DEPTNO', :new.DEPTNO, :old.DEPTNO, :new.rowid);
audit_pkg.check_val( 'dept', 'DNAME', :new.DNAME, :old.DNAME, :new.rowid);
audit_pkg.check_val( 'dept', 'LOC', :new.LOC, :old.LOC, :new.rowid);
end;
/
Trigger created.
ops$tkyte@ORA817DEV.US.ORACLE.COM> update dept set deptno = deptno+1;
1 row updated.
ops$tkyte@ORA817DEV.US.ORACLE.COM> update dept set dname = lower(dname);
1 row updated.
ops$tkyte@ORA817DEV.US.ORACLE.COM> update dept set loc = initcap(loc), dname = upper(dname);
1 row updated.
ops$tkyte@ORA817DEV.US.ORACLE.COM> @printtbl8 'select * from audit_tbl';
TIMESTAMP : 26-feb-2002 12:29:11
WHO : OPS$TKYTE
TNAME : DEPT
CNAME : DEPTNO
OLD : 10
NEW : 11
RID : AAAGl7AABAAAFSaAAA
-----------------
TIMESTAMP : 26-feb-2002 12:29:11
WHO : OPS$TKYTE
TNAME : DEPT
CNAME : DEPTNO
OLD : 20
NEW : 21
RID : AAAGl7AABAAAFSaAAB
-----------------
TIMESTAMP : 26-feb-2002 12:29:11
WHO : OPS$TKYTE
TNAME : DEPT
CNAME : DEPTNO
OLD : 30
NEW : 31
RID : AAAGl7AABAAAFSaAAC
....
-----------------
TIMESTAMP : 26-feb-2002 12:32:56
WHO : OPS$TKYTE
TNAME : DEPT
CNAME : LOC
OLD : NEW YORK
NEW : New York
RID : AAAGl9AABAAAFSaAAA
-----------------
PL/SQL procedure successfully completed.