如何修复oracle中使用触发器插入后更新表时出现的表正在发生变化的异常?

ego6inou  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(198)

我做了一个触发器来更新表“SchemeComp”行,同时插入一个新行。我一次插入多个/批量行,但它抛出一个异常,该行没有被插入。这是我得到的异常:

Error report -
ORA-04091: table ABC009.SchemeComp is mutating, trigger/function may not see it
ORA-06512: at "ABCS009.TrCreateParentChildRelation", line 3
ORA-04088: error during execution of trigger 'ABCS009.TrCreateParentChildRelation' `

字符串
这是我对触发器的查询:

CREATE OR REPLACE TRIGGER "TrCreateParentChildRelation"
AFTER INSERT ON "SchemeComp"
FOR EACH ROW
BEGIN
  IF :NEW."ParentComponentCode" IS NOT NULL THEN
    UPDATE "SchemeComp" sc1
    SET sc1."ParentId" = (
        SELECT sc2."Id"
        FROM "SchemeComp" sc2
        WHERE sc1."ParentComponentCode" = sc2."ComponentCode"
          AND sc1."SchemeId" = sc2."SchemeId"
    )
    WHERE sc1."ParentComponentCode" IS NOT NULL
      AND sc1."SchemeId" = :NEW."SchemeId";
  END IF;
END;
/


下面是我的插入查询,我一次运行两个插入查询。

INSERT INTO "SchemeComp" ("ParentId", "ComponentCode", "ParentComponentCode", "ComponentName", "SchemeId", "EntryDate", "IsActive", "CreatedBy", "IP")
VALUES (null, 'B', null, 'Tribal Area Sub Plan', '0R55', SYSTIMESTAMP, '1', 'BANKADMIN002', '::1');

INSERT INTO "SchemeComp" ("ParentId", "ComponentCode", "ParentComponentCode", "ComponentName", "SchemeId", "EntryDate", "IsActive", "CreatedBy", "IP")
VALUES (null, 'B.1', 'B', 'Recurring', '0R55', SYSTIMESTAMP, '1', 'BANKADMIN002', '::1');

unguejic

unguejic1#

你不能 * 直接 * update(或选择值)表,你插入行,因为它是-如错误所说- * 变异 *。
但是,如果你重写trigger,让你先选择所需的值,然后更新它,那么它应该是好的。像这样的东西:

CREATE OR REPLACE TRIGGER "TrCreateParentChildRelation"
   BEFORE INSERT
   ON "SchemeComp"
   FOR EACH ROW
DECLARE
   l_parentid  "SchemeComp"."ParentId"%TYPE;
BEGIN
   IF :NEW."ParentComponentCode" IS NOT NULL
   THEN
      SELECT sc2."Id"
        INTO l_parentid
        FROM "SchemeComp" sc2
       WHERE     sc2."ComponentCode" = :new."ParentComponentCode"
             AND sc2."SchemeId" = :new."SchemeId";

      :new."ParentId" := l_parentid;
   END IF;
END;

字符串

相关问题