ORA-04091:表是变异的,触发器/函数在执行oracle触发器时可能看不到错误

时间:2015-09-22 11:55:52

标签: oracle triggers sql-insert

我有以下触发器,其中FIELD_NAME字段我想将FIELD_TRACKING表中的值作为'Deactivation time of KPI in case of Downtime(Select KPI_FREQ_TIME_UNIT FROM KPI_DEFINITION)'插入。此字符串值中的括号部分来自KPI_FREQ_TIME_UNIT表的KPI_DEFINITION字段。所以下面是我为此烦恼的触发器。触发器编译没有任何错误。但是,当我尝试更改DNTM_REAC_AFTER_HRS表中的KPI_DEFINITION字段时,我收到错误ORA-04091: table RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION is mutating, trigger/function may not see it ORA-04088: error during execution of trigger 'RATOR_MONITORING_CONFIGURATION.TRG_TRK_KPI_DEFINITION'

create or replace TRIGGER RATOR_MONITORING_CONFIGURATION."TRG_TRK_KPI_DEFINITION" AFTER UPDATE ON RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION
      FOR EACH ROW

    IF NOT  :old.DNTM_REAC_AFTER_HRS=:new.DNTM_REAC_AFTER_HRS THEN
        INSERT INTO RATOR_MONITORING_CONFIGURATION.FIELD_TRACKING  (FIELD_TRACKING_ID,TABLE_NAME,TABLE_ID, FIELD_NAME,FIELD_OLD_VALUE,FIELD_NEW_VALUE,USER_ID, TIMESTAMP, FIELD_TRACKING_COMMENTS)
        VALUES (FIELD_TRACKING_SEQ.NEXTVAL,'KPI_DEFINITION',:new.KPI_DEF_ID,'Deactivation time of KPI in case of Downtime'|| '(' || to_char((Select KPI_FREQ_TIME_UNIT FROM KPI_DEFINITION)) || ')',to_char(:old.DNTM_REAC_AFTER_HRS),to_char( :new.DNTM_REAC_AFTER_HRS),:new.LAST_UPDATED_BY,:new.LAST_UPDATED_DATE, decode(:new.KPI_ACTIVE_DOWNTIME,'N','This KPI has been reactivated on end of a downtime.',''));
      END IF;

    END;

2 个答案:

答案 0 :(得分:3)

触发器无法读取表(选择KPI_FREQ_TIME_UNIT FROM KPI_DEFINITION),这会改变...您可以通过以下方式访问该值:: new.KPI_FREQ_TIME_UNIT。 更多信息:http://www.dba-oracle.com/t_avoiding_mutating_table_error.htm

在其他情况下,您可以尝试在autonomous transaction中执行此操作:

create or replace TRIGGER RATOR_MONITORING_CONFIGURATION."TRG_TRK_KPI_DEFINITION" 
AFTER UPDATE ON RATOR_MONITORING_CONFIGURATION.KPI_DEFINITION FOR EACH ROW
DECLARE
   PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  -- ...
  COMMIT; -- don't forget it!!!
END;

答案 1 :(得分:-1)

由于您需要为您创建触发器的表中的信息(选择KPI_FREQ_TIME_UNIT FROM KPI_DEFINITION),您可以从参考:NEW 获取KPI_FREQ_TIME_UNIT,因为它代表新行(:NEW.KPI_FREQ_TIME_UNIT