我在包含审计日志的表上创建了一个触发器。插入新条目时,我想逐列将新记录与表中的最新现有记录(匹配 ID)进行比较。如果一列的值发生了变化,我想在另一个表中执行插入语句。我创建了以下 PL SQL 过程:
CREATE OR REPLACE
TRIGGER PROJECT_BASELINE_ATTR_AUD_BI BEFORE INSERT
ON PROJECT_BASELINE_ATTR_AUD
FOR EACH ROW
DECLARE
tab_name constant varchar2(32) := 'PROJECT_BASELINE_ATTR_AUD';
col_name varchar2(32);
v_latest_row PROJECT_BASELINE_ATTR_AUD%rowtype;
BEGIN
-- get the record to compare with
EXECUTE IMMEDIATE 'select * from '||tab_name||
' where rev = (select max(rev) from '||tab_name||' where id = '|| :new.ID ||')' into v_latest_row;
FOR x IN (SELECT DISTINCT(COLUMN_NAME) FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = upper(tab_name))
LOOP
BEGIN
col_name := x.column_name;
-- do insert if the values are unequal
-- IF :new.col_name <> v_latest_row.col_name
-- INSERT INTO AUD_CHANGE_LOG VALUES (AUD_CHANGE_LOG_ID_SEQ.nextVal, :new.ID, :new.REV, tab_name, col_name);
END;
END LOOP;
END;
我知道我可以用真实的列名来实现它,例如
IF :new.FOO <> v_latest_row.FOO
IF :new.BAR <> v_latest_row.BAR
...
但这将在很长的情况下结束。有任何想法吗?