监控某个表的insert,update,delete的操作

今天遇到个问题,不知道那部操作在一张表中insert,把项目翻了个低产田也无法找到,最后没有办法,只好用触发器来监控某个表的insert,update,delete的操作,具体方法如下,供大家参考参考!

第一步,使用sys授权

如在orcl用户下监控其表上的操作。

Grant select any dictionary to orcl;

第二步,

监控对象:

create table t1_trg as select * from user_objects;

第三步,

创建日志表

CREATE TABLE t1_trg_log (

sql_text VARCHAR2(1000),

IPADDR varchar2(50),

sid NUMBER,

serial# NUMBER,

timestamp DATE,

username VARCHAR2(30),

osuserid VARCHAR2(30),

machinename VARCHAR2(64),

PROGRAMname VARCHAR2(64)

) tablespace users;

第四步,创建监控触发器

create or replace trigger trg_t1_trg_log

after insert or delete or update on t1_trg

DECLARE

v_sql_text varchar2(1000);

machinename VARCHAR2(64);

osuserid VARCHAR2(30);

v_sid NUMBER(10);

v_serial NUMBER(10);

v_IPADDR varchar2(50);

v_PROGRAMname varchar2(50);

CURSOR c1 IS

SELECT s.sql_text,

SYS_CONTEXT('USERENV', 'IP_ADDRESS') ipaddr,

sid,

serial#,

osuser,

machine,

program

FROM v$session v ,gv$sql s

WHERE v.sql_id=s.sql_id(+) and v.sid = (SELECT sid FROM gv$mystat WHERE rownum = 1);

BEGIN

OPEN c1;

FETCH c1

INTO v_sql_text, v_ipaddr, v_sid, v_serial, osuserid, machinename, v_PROGRAMname;

INSERT INTO t1_trg_log

(sql_text,

IPADDR,

SID,

SERIAL#,

TIMESTAMP,

USERNAME,

OSUSERID,

MACHINENAME,

PROGRAMNAME)

VALUES

(v_sql_text,

v_ipaddr,

v_sid,

v_serial,

sysdate,

user,

osuserid,

machinename,

v_PROGRAMname);

CLOSE c1;

END;

你可能感兴趣的:(sql)