oracle数据库 使用触发器监控BK_USER_ACTIVE表的增删改操作并将其记录到trig_sql表中
使用的SQL脚本如下:
1.创建trig_sql表用于记录BK_USER_ACTIVE表的增删改操作
create table trig_sql(
LT DATE not null primary key,
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)
);
2.创建trig_sql索引
create index idx_time on trig_sql (LT);
3.创建触发器pri_test BK_USER_ACTIVE为要监控的表
create or replace trigger pri_test
after insert or update or delete on BK_USER_ACTIVE for each row
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF inserting THEN
INSERT INTO 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 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 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;
SQL执行后对BK_USER_ACTIVE执行增删改操作 触发器执行错误信息提示如下图
ORA-04098: trigger 'JIZHANG.PRI_TEST' is invalid and failed re-validation
, Time: 0.071000s
用如下SQL脚本查看触发器执行失败详细信息
select * from SYS.USER_ERRORS where NAME = upper('pri_test');
触发器执行失败详细信息
PRI_TEST TRIGGER 1 10 25 PL/SQL: ORA-00942: table or view does not exist ERROR 0
PRI_TEST TRIGGER 2 5 5 PL/SQL: SQL Statement ignored ERROR 0
网上调查后,个人认定失败是oracle数据库用户权限问题导致(oracle用户继承自角色的权限不包含存储过程,函数,触发器等特殊对象)
网上方案:需要重新对用户显示赋权(trigger相关)
但在网上并没有找到能解决我问题的具体方案。遂发布问答咨询网友,请不吝赐教!
(所使用脚本皆来源网络,如有版权问题,请及时联系本人~
参考资料:https://www.cnblogs.com/lm970585581/p/9138050.html)