SQL>
SQL> describe plas.trig_sql;
Name Null? Type
----------------------------------------- -------- ----------------------------
LT DATE
SID NUMBER
SERIAL# NUMBER
USERNAME VARCHAR2(30)
OSUSER VARCHAR2(64)
MACHINE VARCHAR2(32)
TERMINAL VARCHAR2(16)
PROGRAM VARCHAR2(64)
SQLTEXT VARCHAR2(2000)
STATUS VARCHAR2(30)
CLIENT_IP VARCHAR2(60)
SQL>
2,并且在 trig_sql表上面添加索引:
create index idx_time on plas.trig_sql (LT);
3,建立触发器
create or replace trigger pri_test
after insert or update or delete on plas.aaa_test
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF inserting THEN
INSERT INTO plas.trig_sql
select sysdate,s.SID, s.SERIAL#, s.USERNAME, s.OSUSER,
s.MACHINE, s.TERMINAL, s.PROGRAM, q.sql_text line,
'INSERT',
sys_context('userenv','ip_address')
from v$sql q, v$session s
where s.audsid=(select userenv('SESSIONID') from dual)
and s.prev_sql_addr=q.address
AND s.PREV_HASH_VALUE = q.hash_value;
COMMIT;
ELSIF deleting then
INSERT INTO plas.trig_sql
select sysdate,s.SID, s.SERIAL#, s.USERNAME, s.OSUSER,
s.MACHINE, s.TERMINAL, s.PROGRAM, q.sql_text line,
'DELETE',
sys_context('userenv','ip_address')
from v$sql q, v$session s
where s.audsid=(select userenv('SESSIONID') from dual)
and s.prev_sql_addr=q.address
AND s.PREV_HASH_VALUE = q.hash_value;
COMMIT;
ELSIF updating then
INSERT INTO plas.trig_sql
select sysdate,s.SID, s.SERIAL#, s.USERNAME, s.OSUSER,
s.MACHINE, s.TERMINAL, s.PROGRAM, q.sql_text line,
'UPDATE',
sys_context('userenv','ip_address')
from v$sql q, v$session s
where s.audsid=(select userenv('SESSIONID') from dual)
and s.prev_sql_addr=q.address
AND s.PREV_HASH_VALUE = q.hash_value;
COMMIT;
END IF;
END;
4,开始进行数据操作测试:
insert into plas.aaa_test1 select 2,'tom',sysdate from dual;
update plas.aaa_test1 a set a.name='tom_up' where a.id=2;
update plas.aaa_test a set a.name='tom_up1' where a.id=1;
......
commit;
5,去查看表记录,会发现如下
SQL> select * from plas.trig_sql;
LT SID SERIAL# USERNAME OSUSER MACHINE TERMINAL PROGRAM SQLTEXT STATUS CLIENT_IP
----------- ---------- ---------- ------------------------------ ---------------------------------------------------------------- -------------------------------- ---------------- ---------------------------------------------------------------- -------------------------------------------------------------------------------- ------------------------------ ------------------------------------------------------------
2014/10/29 1273 33297 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; INSERT 192.168.170.180
2014/10/29 1273 33297 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; DELETE 192.168.170.180
2014/10/29 1273 33297 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; INSERT 192.168.170.180
2014/10/29 1352 40155 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; INSERT 192.168.170.180
2014/10/29 1273 33297 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; UPDATE 192.168.170.180
2014/10/29 1273 33297 POWERDESK Administrator WORKGROUP\WIN-18P5Q5AREH9 WIN-18P5Q5AREH9 plsqldev.exe begin :id := sys.dbms_transaction.local_transaction_id; end; UPDATE 192.168.170.110
2014/10/29 25 39527 SYS oracle localhost.localdomain pts/1 sqlplus@localhost.localdomain (TNS V1-V3) update plas.aaa_test a set a.name='tom_update' where id=2 UPDATE
2014/10/29 25 39527 SYS oracle localhost.localdomain pts/1 sqlplus@localhost.localdomain (TNS V1-V3) update plas.aaa_test a set a.name='tom_update3' where id=3 UPDATE
8 rows selected
SQL>