Oracle - 更新后触发

时间:2017-11-16 14:15:06

标签: oracle triggers

我有触发器:

CREATE OR REPLACE TRIGGER TR.TR_OUTPUT_EHT_UPDATE_OPE
  AFTER DELETE
  ON TR.TR_JN_OUTPUT_EHT
  FOR EACH ROW
DECLARE
   BEGIN
  UPDATE TR.TR_BP tbp
    SET tbp.DONE= 2, tbp.DATA_WYK = SYSDATE
    WHERE EXISTS (SELECT ARCH.NR
    FROM TR.TR_KB_OUTPUT_ARCH ARCH
        WHERE ARCH.NR = tbp.NRKK)
    AND NOT EXISTS (SELECT OUTPUT.NR_SERYJNY
        FROM TR.TR_JN_OUTPUT OUTPUT
        WHERE OUTPUT.NR = tbp.NRKK);
END;
/

之后我想得到" NRKK"刚更新的行并更新另一个由此" NRKK":

连接的表
UPDATE P.TR_OPE OPE
SET OPE.DATA = SYSDATE, 
OPE.STATUS = OPE.STATUS + 1 
WHERE OPE.NRKK = "NRKK"

我试着这样做:

SELECT tbp.NRKK INTO v_nrkk FROM TR.TR_BP tbp WHERE tbp.DATA = (SELECT 
MAX(tbp.DATA) FROM TR.TR_BP tbp1);

但我有错误:ORA 04091,ORA 06512和ORA 04088。

编辑:

我创建了另一个触发器:

CREATE OR REPLACE TRIGGER TR.TR_OP_E
  AFTER UPDATE ON TR.TR_BP
FOR EACH ROW
DECLARE
    v_nrkk TR.TR_BP.NRKK % TYPE;
BEGIN
  SELECT tbp.NRKK INTO v_nrkk FROM TR.TR_BPtbp WHERE tbp.DATA = (SELECT 
MAX(tbp.DATA) FROM TR.TR_BP tbp1);

  IF v_nrkk is NOT NULL THEN
  UPDATE P.TR_OPE OPE
    SET OPE.DATA = SYSDATE, OPE.STATUS = 1
    WHERE OPE.ID_BR = '00100300'
    AND OPE.NRKK = v_nrkk;
  END IF;
  END;
/ 

但我还有错误......

1 个答案:

答案 0 :(得分:1)

如@Renato Afonso所述,您可以避免使用COMPOUND TRIGGER来改变表错误。见下面的演示;

- 表

CREATE TABLE TABLE_1 (data NUMBER);
/
CREATE TABLE TABLE_2 (DATA1 NUMBER);
/

- 在table_1中记录

SQL> select * from TABLE_1;

      DATA
----------
         1

- 在table_2中记录

SQL> select * from TABLE_2;

     DATA1
----------
         5

触发:

CREATE OR REPLACE TRIGGER TR_OP_E FOR
   INSERT OR UPDATE OR DELETE
   ON TABLE_1
   COMPOUND TRIGGER
   v_nrkk   TABLE_1.DATA%TYPE;
   AFTER STATEMENT
   IS
   BEGIN
      --Selecting from the same table on which trigger is created
      SELECT tbp.data
        INTO v_nrkk
        FROM Table_1 tbp
       WHERE tbp.DATA = (SELECT MAX (tbp.DATA)
                           FROM Table_1 tbp1);

      IF v_nrkk IS NOT NULL
      THEN
         UPDATE TABLE_2 OPE
            SET OPE.DATA1 = v_nrkk;
      END IF;
   END
   AFTER STATEMENT;

END;
/

执行:

SQL> UPDATE TABLE_1 SET DATA = 2;

1 row updated.

SQL> COMMIT;

Commit complete.

SQL> select * from TABLE_1;

      DATA
----------
         2

SQL>  select * from TABLE_2;

     DATA1
----------
         2