-- Create tablecreate table Z_TRIG_SYS(ltDATE,sid NUMBER,serial# NUMBER,usernameVARCHAR2(30),oSUSErVARCHAR2(64),machine VARCHAR2(32),terminalVARCHAR2(16),object_name VARCHAR2(200),ora_syseventVARCHAR2(200),program VARCHAR2(64),sqltext VARCHAR2(4000),statusVARCHAR2(30),client_ip VARCHAR2(60),ora_dbnameVARCHAR2(60),ora_client_ip_address VARCHAR2(60));-- Add comments to the columns comment on column Z_TRIG_SYS.ltis "录入时间";comment on column Z_TRIG_SYS.sidis "当前session的id";comment on column Z_TRIG_SYS.serial#is "sid的序列号,顺序自增";comment on column Z_TRIG_SYS.usernameis "登录的用户名";comment on column Z_TRIG_SYS.osuseris "操作者的os系统";comment on column Z_TRIG_SYS.machineis "操作者的机器名称";comment on column Z_TRIG_SYS.object_nameis "操作对象名称";comment on column Z_TRIG_SYS.ora_syseventis "操作事件";comment on column Z_TRIG_SYS.sqltextis "执行的sql片段";comment on column Z_TRIG_SYS.client_ipis "客户端ip";comment on column Z_TRIG_SYS.ora_dbnameis "执行的数据库";comment on column Z_TRIG_SYS.ora_client_ip_addressis "客户端ip地址";
3.3 建立system级别触发器
create or replace trigger trig_systemafter drop on databasebeginif ora_login_user!="system" then insert into z_trig_sys( lt,sid ,serial# ,username,osuser,machine ,terminal,object_name ,ora_sysevent,program ,sqltext ,status,client_ip ,ora_dbname,ora_client_ip_address )select sysdate, s.SID,s.SERIAL#,s.USERNAME,s.OSUSER, s.MACHINE, s.TERMINAL, s.PROGRAM, ora_dict_obj_name, ora_sysevent, "drop object on database", "",sys_context("userenv","ip_address"),ora_database_name,ora_client_ip_addressfrom 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; -- AND sys_context("userenv","ip_address") !="192.168.180.106"; end if;end trig_system;
3.4,调试报错
ORA-00604: error occurred at recursive SQL level 1 ORA-04092: cannot COMMIT in a trigger去掉触发器中的commit;