【问题标题】:update in trigger causing recursive loop when call a function调用函数时触发器中的更新导致递归循环
【发布时间】:2016-06-24 19:40:16
【问题描述】:

我创建了一个具有如下自我关系的表:

创建表组织(id,name,parent_id)

为了方便访问,我将组织二进制代码添加到表中,并添加触发器以在更改父级后更新组织二进制代码。 当我想更新时,我会进行递归循环并从 oracle 获取死锁。

我在一个单独的函数中更新代码并在触发器结束时调用它; 我这样循环:

  1. 更新记录
  2. 运行触发器
  3. 更新组织代码
  4. 运行触发器
  5. 更新组织代码 以此类推

【问题讨论】:

  • 能发一下触发代码吗?
  • 你为什么要这样做? ELSE :new.hierarchicode := :old.hierarchicode;?
  • 这是一个错误,但问题不在于那个
  • 好的,请编辑您的问题,包括代码。

标签: oracle recursion triggers


【解决方案1】:
  CREATE OR REPLACE TRIGGER TRG_Core_Organization_before
  before insert OR UPDATE on Core_Organization
 for each row

declare
-- local variables here
  parent_HIERARCHICODE number;
  PRAGMA AUTONOMOUS_TRANSACTION;

begin

  IF INSERTING OR (UPDATING AND :new.parentid != :old.parentid) OR
     (UPDATING AND :old.hierarchicode IS NULL) THEN
    Begin
      -- get last code in this root 

  --   RAISE_APPLICATION_ERROR(-20001,'ERROR HIERARCHICODE');

  select HIERARCHICODE
    into parent_HIERARCHICODE
    from Core_Organization cp
   where cp.id = :new.parentid;

  select NVL(max(HIERARCHICODE) + 1, parent_HIERARCHICODE || '001')
    into :new.hierarchicode
    from Core_Organization cp
   where cp.parentid = :new.parentid;

EXCEPTION
  WHEN NO_DATA_FOUND THEN
    :new.hierarchicode := cast(parent_HIERARCHICODE || '001' as number);
END;

  END IF;
end;

【讨论】:

    猜你喜欢
    • 2015-01-07
    • 1970-01-01
    • 2017-03-14
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多