Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Create or Replace TRIGGER audit_s_emp_changes
Before insert or update or delete on test.s_emp FOR EACH ROW
BEGIN
if inserting then
generic_audit('test.s_emp','INSERT','All columns',:new.id,'inserting','INSERTING');
elsif deleting then
generic_audit('test.s_emp','DELETE','All columns',:old.id,'deleting','DELETING');
else
generic_audit('test.s_emp','UPDATE','LAST_NAME',:old.id,:old.last_name,:new.last_name);
generic_audit('test.s_emp','UPDATE','FIRST_NAME',:old.id,:old.FIRST_name,:new.FIRST_name);
generic_audit('test.s_emp','UPDATE','SALARY',:old.id,:old.SALARY,:new.SALARY);
end if;
end;
/
REM **************************************************************
REM David L. Hunt (file author) distributes this and other
REM files/scripts for educational purposes only, to illustrate the
REM use or application of various computing techniques. Neither the
REM author nor Dasages, LLC, makes any warranty regarding this
REM script's fitness for any industrial application or purpose nor is
REM there any claim that this or any similarly-distributed scripts
REM are error free or should be used for any purpose other than
REM illustration.
REM **************************************************************
Create or Replace procedure generic_audit
(table_name in varchar2,
action_type in varchar2,
updated_column in varchar2,
pk in varchar2,
old_value in varchar2,
new_value in varchar2)
is
userinfo varchar2(200);
begin
IF (old_value is null and new_value is not null) or
(old_value is not null and new_value is null) or
(old_value <> new_value) THEN
select osuser||' on '||machine||'(as '||user||')' into userinfo
from sys.sessions where audsid = userenv('sessionid');
INSERT INTO auditor
VALUES (table_name, SYSDATE,USERINFO,action_type,
pk,updated_column,old_value,new_value);
END IF;
END;
/
Note: the above stored procedure (as written) depends upon SELECT privileges on the SYS.SESSIONS table.
create table auditor
(table_name varchar(30)
,action_datetime date
,perpetrator varchar(200)
,action_type varchar(10)
,pk varchar(100)
,which_column varchar(30)
,old_value varchar(4000)
,new_value varchar(4000)
)
/
insert into s_emp (id, last_name, first_name, salary) values (28,'98','Acct',9500);
commit;
update s_emp set salary = 9600 where id = 28;
commit;
update s_emp set last_name = '99' where id = 28;
commit;
delete s_emp where id = 28;
commit;
/
col a heading "Table|Name" format a15
col b heading "Date/Time|of Change" format a11
col c heading "Identity of Changer" format a20
col d heading "Action|Type" format a7
col e heading "Primary|Key or|Unique|ID" format a10
col f heading "Column|Name" format a20
col g heading "Old Value" format a20
col h heading "New Value" format a20
set echo off
set linesize 200
set pagesize 500
select TABLE_NAME a
,to_char(ACTION_DATETIME,'dd-MON-yyyy hh24:mi:ss') b
,PERPETRATOR c
,ACTION_TYPE d
,PK e
,WHICH_COLUMN f
,OLD_VALUE g
,NEW_VALUE h
from auditor
order by action_datetime
/
(Note: On Tek-Tips, the following output appears with undesirable display wrapping; for proper viewability, copy and paste to a file that does not have right margin limitations of less than 91 character positions.)
Primary
Key or
Table Date/Time Action Unique Column
Name of Change Identity of Changer Type ID Name Old Value New Value
---------- ----------- --------------------- ------ ------- ----------- --------- ---------
test.s_emp 22-MAR-2004 dhunt on TFMOBILE2709 INSERT 28 All columns inserting INSERTING
09:39:29 (as TEST)
test.s_emp 22-MAR-2004 dhunt on TFMOBILE2709 UPDATE 28 SALARY 9500 9600
09:39:29 (as TEST)
test.s_emp 22-MAR-2004 dhunt on TFMOBILE2709 UPDATE 28 LAST_NAME 98 99
09:39:30 (as TEST)
test.s_emp 22-MAR-2004 dhunt on TFMOBILE2709 DELETE 28 All columns deleting DELETING
09:42:19 (as TEST)