-1

要求:一旦对表中的列值进行任何更新OPS_BUILD_ACTIVITY_LABEL,我们必须在表中插入一行,该行的ACTIVITY_LABEL_AUDIT_LOG列为TABLE_NAME, COLUMN_NAME, OLD_VALUE, NEW_VALUE, UPDATED_BY, UPDATED_DTM

我创建了如下触发器。使用以下编译错误创建的触发器:

PL/SQL: SQL Statement ignored
PL/SQL: ORA-00984: column not allowed here

请帮助我。

create or replace TRIGGER ACTIVITY_LABEL_TRIGGER
AFTER UPDATE OF ACTIVITY_LABEL, NOTES
ON OPS_BUILD_ACTIVITY_LABEL
FOR EACH ROW
BEGIN
  INSERT INTO ACTIVITY_LABEL_AUDIT_LOG
   ( TABLE_NAME,
     COLUMN_NAME,
     OLD_VALUE,
     NEW_VALUE,
     UPDATED_BY,
     UPDATED_DTM)   
     VALUES   

   ('OPS_BUILD_ACTIVITY_LABEL',
    'ACTIVITY_LABEL',
    OLD.ACTIVITY_LABEL,
    NEW.ACTIVITY_LABEL,
    NEW.LAST_UPDATED_BY,
    NEW.LAST_UPDATED_DTM); 

END;
4

2 回答 2

0

只需在 OLD 和 NEW 前面加上冒号(:),如下所示(ORA-00984会因此而引发,顺便说一下,我认为列名没有问题):

CREATE OR REPLACE TRIGGER ACTIVITY_LABEL_TRIGGER
AFTER ON OPS_BUILD_ACTIVITY_LABEL
FOR EACH ROW
BEGIN
 if ( nvl(:OLD.ACTIVITY_LABEL,'xYz#@!') != nvl(:NEW.ACTIVITY_LABEL,'xYz#@!')
   or nvl(:OLD.NOTES,'xYz#@!') != nvl(:NEW.NOTES,'xYz#@!') ) then
  INSERT INTO ACTIVITY_LABEL_AUDIT_LOG
    (TABLE_NAME,
     COLUMN_NAME,
     OLD_VALUE,
     NEW_VALUE,
     UPDATED_BY,
     UPDATED_DTM)
  VALUES  
    ('OPS_BUILD_ACTIVITY_LABEL',
     'ACTIVITY_LABEL',
     :OLD.ACTIVITY_LABEL,
     :NEW.ACTIVITY_LABEL,
     :NEW.LAST_UPDATED_BY,
     :NEW.LAST_UPDATED_DTM);
 end if;
END;

关于您的评论,我删除了该部分并在IF 语句UPDATE OF ACTIVITY_LABEL, NOTES中对两列进行比较。如果要获取每个更新操作的每个日志,还应该删除IF 语句ACTIVITY_LABELNOTES

于 2018-05-30T11:22:39.677 回答
0

感谢您的所有回答,根据您的回答,我终于通过以下代码实现了要求:

create or replace TRIGGER ACTIVITY_LABEL_TRIGGER
AFTER UPDATE OF ACTIVITY_LABEL, NOTES
ON OPS_BUILD_ACTIVITY_LABEL
FOR EACH ROW
BEGIN
 if ( nvl(:OLD.ACTIVITY_LABEL,'xYz#@!') != nvl(:NEW.ACTIVITY_LABEL,'xYz#@!')) then
  INSERT INTO ACTIVITY_LABEL_AUDIT_LOG
    (TABLE_NAME,
     COLUMN_NAME,
     OLD_VALUE,
     NEW_VALUE,
     UPDATED_BY,
     UPDATED_DTM)
  VALUES  
    ('OPS_BUILD_ACTIVITY_LABEL',
     'ACTIVITY_LABEL',
     :OLD.ACTIVITY_LABEL,
     :NEW.ACTIVITY_LABEL,
     :NEW.LAST_UPDATED_BY,
     :NEW.LAST_UPDATED_DTM);
 end if;

 if (nvl(:OLD.NOTES,'xYz#@!') != nvl(:NEW.NOTES,'xYz#@!') ) then
  INSERT INTO ACTIVITY_LABEL_AUDIT_LOG
    (TABLE_NAME,
     COLUMN_NAME,
     OLD_VALUE,
     NEW_VALUE,
     UPDATED_BY,
     UPDATED_DTM)
  VALUES  
    ('OPS_BUILD_ACTIVITY_LABEL',
     'NOTES',
     :OLD.NOTES,
     :NEW.NOTES,
     :NEW.LAST_UPDATED_BY,
     :NEW.LAST_UPDATED_DTM);
 end if;
END; 
于 2018-05-30T12:34:13.907 回答