虽然我的第一个答案有效,但它在很大程度上取决于表列,如果必须审核许多表,则它实际上不可用。基于@Sayan Malakshinov 的评论和链接的文章StevenFeuerstein&CompoundTrigger。如果在一次更新中处理了许多行,最终的审计循环可能会导致性能问题.... 批量更新可以移动到更优化的更新复合触发器,用一个“插入到 tblaudit( ) 从 tblone 中选择 auditColumns,其中 id 在(记录的 id´s)"
-- prepare test-scenario
CREATE TABLE TBLONE
(
ID NUMBER(19),
POSTALCODE VARCHAR2(20 CHAR),
STREET VARCHAR2(255 CHAR),
HOUSENUMBER VARCHAR2(25 CHAR),
CITY VARCHAR2(255 CHAR)
)
;
ALTER TABLE TBLONE ADD (
PRIMARY KEY
(ID)
USING INDEX);
insert into tblone values (1,'123123','street1','1a','DevVille1');
insert into tblone values (2,'345','street2','2b','DevVille2');
insert into tblone values (3,'678','street3','3c','DevVille3');
CREATE TABLE TBLAUDIT
(
ID NUMBER(19),
log_action VARCHAR2(10 CHAR),
log_user varchar(100 char),
log_data clob
)
;
-- ... timestamp of modification might be missing ...
CREATE OR REPLACE TRIGGER trg_audit_tblone
FOR UPDATE OR INSERT OR DELETE ON tblone
COMPOUND TRIGGER
TYPE id_rt IS RECORD (
id_column NUMBER(19) -- assuming all primary-keys are of same type ....
,log_action varchar(10)
);
TYPE row_level_info_t IS TABLE OF id_rt INDEX BY PLS_INTEGER;
g_row_level_info row_level_info_t;
AFTER EACH ROW IS
BEGIN
g_row_level_info (g_row_level_info.COUNT + 1).id_column := :NEW.id; -- store primary key only
-- remember the type of trigger-action
IF INSERTING THEN
g_row_level_info (g_row_level_info.COUNT).log_action := 'Insert';
ELSIF UPDATING THEN
g_row_level_info (g_row_level_info.COUNT).log_action := 'Update';
ELSIF DELETING THEN
g_row_level_info (g_row_level_info.COUNT).log_action := 'Delete';
end if;
END AFTER EACH ROW;
AFTER STATEMENT IS
l_rowdata clob;
BEGIN
-- for all row-actions recorded
FOR indx IN 1 .. g_row_level_info.COUNT
LOOP
if g_row_level_info (indx).log_action= 'Delete' then
-- no row selectable, direct insert to audit
insert into TBLAUDIT values(g_row_level_info(indx).id_column,sys_context('USERENV','SESSION_USER') , g_row_level_info (indx).log_action,null);
else
select xmltype(cursor(select * from tblone where id=g_row_level_info (indx).id_column)).getclobval() into l_rowdata from dual;
insert into TBLAUDIT values(g_row_level_info(indx).id_column,sys_context('USERENV','SESSION_USER') , g_row_level_info (indx).log_action,l_rowdata);
end if;
END LOOP;
END AFTER STATEMENT;
END trg_audit_tblone;
update tblone
set street='newStreet'
where id=1;
select id,log_action,log_user,dbms_lob.substr(log_data,200) from tblaudit;
update tblone
set street='newStreet';
select id,log_action,log_user,dbms_lob.substr(log_data,200) from tblaudit;
delete from tblone where id=2;
select id,log_action,log_user,dbms_lob.substr(log_data,200) from tblaudit;
如果确实必须考虑使用多行进行更新:
create TYPE type_audit_entry as object (
id_column NUMBER(19) -- assuming all primary-keys are of same type ....
,log_action varchar(10)
);
create type typeTable_audit as table of type_audit_entry;
CREATE OR REPLACE TRIGGER trg_audit_update_tblone
FOR UPDATE ON tblone
COMPOUND TRIGGER
g_row_level_info typeTable_audit := typeTable_audit();
BEFORE STATEMENT IS
BEGIN
g_row_level_info:=typeTable_audit();
END BEFORE STATEMENT;
AFTER EACH ROW IS
BEGIN
g_row_level_info.extend;
g_row_level_info (g_row_level_info.COUNT):=type_audit_entry(:NEW.id,null);
END AFTER EACH ROW;
AFTER STATEMENT IS
l_rowdata clob;
BEGIN
insert into TBLAUDIT(ID,log_action,log_user,log_time,log_data)
select stored_audit.id_column,'Update',sys_context('USERENV','SESSION_USER'),sysdate,xmltype(cursor(select * from tblone where id=stored_audit.id_column)).getclobval()
from TABLE(g_row_level_info) stored_audit;
END AFTER STATEMENT;
END trg_audit_update_tblone;
delete from tblaudit;
update tblone
set street='newStreet';
select id,log_action,log_time,log_user,dbms_lob.substr(log_data,200) from tblaudit;