Oracle中触发器中的案例声明

时间:2017-01-21 11:17:40

标签: oracle plsql triggers

我在运行时遇到触发器问题。它成功编译没有错误,但在尝试运行它时,它会抛出错误ORA-04091 table is mutating。我正在尝试在将新记录添加到表(ATTDETAIL)时在表(ATTD_DETAILS)中插入一行。 并且必须使用新格式替换EADEMPID

触发

CREATE OR REPLACE TRIGGER ATTENDANCE
AFTER INSERT ON ATTD_DETAILS
FOR EACH ROW
 DECLARE
    EMCODE VARCHAR2(50) := 'NA';
 BEGIN

      SELECT CASE
     WHEN EAD.EADEMPID LIKE 'A%' THEN
      '1V' || EADEMPID
     WHEN EAD.EADEMPID LIKE 'C%' THEN
      'LM' || EADEMPID
     WHEN EAD.EADEMPID LIKE 'S%' THEN
      'LM' || EADEMPID
      ELSE EAD.EADEMPID  
      END INTO EMCODE
    FROM ATTD_DETAILS EAD
    WHERE EAD.EADEMPID = :NEW.EADEMPID;

  IF (:NEW.EADREMARKS = 'Successful' OR :NEW.EADREMARKS = 'SUCCESSFUL') THEN
  INSERT INTO ATTDETAIL
  (CODE,
   CARDID,
   ATT_DATE,
   ATT_TIME,
   EMPID,
   PROCODE)
   VALUES
   (:NEW.EADSITECODE,
   :NEW.EADEMCARDID,
   :NEW.EADATT_DATE,
   :NEW.EADATT_TIME,
   EMCODE,
   '880');
  END IF;
  END;

1 个答案:

答案 0 :(得分:2)

假设您要根据刚插入的行向emcode提供值,则需要检查:NEW记录中的列。您无法从触发表中进行选择(在行级触发器中)。

我想要你想要的是:

CREATE OR REPLACE TRIGGER ATTENDANCE
AFTER INSERT ON ATTD_DETAILS
FOR EACH ROW
 DECLARE
    EMCODE VARCHAR2(50) := 'NA';
 BEGIN

     emcode := CASE
       WHEN :new.EADEMPID LIKE 'A%' THEN '1V' || :new.EADEMPID
       WHEN :new.EADEMPID LIKE 'C%' THEN 'LM' || :new.EADEMPID
       WHEN :new.EADEMPID LIKE 'S%' THEN 'LM' || :new.EADEMPID
       ELSE :new.EADEMPID
      END;

  IF (:NEW.EADREMARKS = 'Successful' OR :NEW.EADREMARKS = 'SUCCESSFUL') THEN
  INSERT INTO ATTDETAIL
  (CODE,
   CARDID,
   ATT_DATE,
   ATT_TIME,
   EMPID,
   PROCODE)
   VALUES
   (:NEW.EADSITECODE,
   :NEW.EADEMCARDID,
   :NEW.EADATT_DATE,
   :NEW.EADATT_TIME,
   EMCODE,
   '880');
  END IF;
END;