Oracle Apex:从表A中的触发器调用表B中的触发器

时间:2017-06-28 18:51:28

标签: oracle triggers oracle-apex

我正在尝试从表A执行触发操作,导致表B中的触发操作,但是我收到以下错误。

ORA-04091: table IMIT.INFRA_ASSETS_MASTER is mutating, trigger/function may not see it
ORA-06512: at "IMIT.SUBNET_LOOKUP_DELETE_TRIGGER", line 15
ORA-04088: error during execution of trigger 'IMIT.SUBNET_LOOKUP_DELETE_TRIGGER'
ORA-06512: at "IMIT.INFRA_MASTER_DELETE_TRIGGER", line 2
ORA-04088: error during execution of trigger 'IMIT.INFRA_MASTER_DELETE_TRIGGER'

所以当我从表中删除一个项目时,它会调用此触发器

CREATE OR REPLACE EDITIONABLE TRIGGER  "INFRA_MASTER_DELETE_TRIGGER" 
   before delete on infra_assets_master
   for each row
begin
   DELETE FROM SUBNET_LOOKUP WHERE LINK_ID = :old.ID AND DEF_ID = 3;
   INSERT INTO HISTORY (IT_TABLE,ITEM_ID,ACTION_TYPE,ACTION)VALUES('INFRA_ASSETS_MASTER',:old.ID,3,CONCAT(CONCAT('Infrastructure Asset  ', :old.ASSET_ID),' deleted'));
end;

将最终删除另一个表中的另一个记录,调用该表中的删除触发器

CREATE OR REPLACE EDITIONABLE TRIGGER  "SUBNET_LOOKUP_DELETE_TRIGGER" 
   before delete on subnet_lookup
   for each row
declare
  v_def varchar2(255);
  v_link varchar2(255);
  v_subnet varchar2(255);
  v_sen varchar2(255);
begin
   select NAME into v_def from subnet_lookup_definition where id = :old.DEF_ID;
   v_sen := 'Remove Link Between ';
   if :old.DEF_ID = 1 then
      select hostname into v_link from servers_master where id = :old.LINK_ID;
      v_sen := concat(v_sen,concat(concat('Server ''', v_link),''''));
   end if;

   if :old.DEF_ID = 3 then
      select asset_id into v_link from infra_assets_master where id = :old.LINK_ID;
      if v_link IS NULL then
        select hostname into v_link from infra_assets_master where id = :old.LINK_ID;
      end if;
      v_sen := concat(v_sen,concat(concat('Infrastructure Assets ''', v_link),''''));
   end if;

   if :old.DEF_ID = 4 then
      select asset_id into v_link from assets_master where id = :old.LINK_ID;
      v_sen := concat(v_sen,concat(concat('IT Assets ''', v_link),''''));
   end if;

   select address into v_subnet from subnet where id = :old.SUBNET_ID;

   v_sen := concat(concat(v_sen, concat(' to ', v_subnet)),' has been deleted');

   insert into HISTORY (IT_TABLE, ITEM_ID,ACTION_TYPE, ACTION) VALUES ('SUBNET_LOOKUP',:old.SUBNET_ID,3,v_sen);
end;

我不明白这是什么问题。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

  

"问题是什么"

删除infra_assets_master时,表格上的触发器会删除subnet_lookup中的子记录。但该表还有一个查找其父infra_assets_master记录的触发器。哪个您要删除的记录。 Oracle并不了解如何解决这种混淆,因此它会引发变异表错误。

  

"如何解决此问题?"

通过删除循环依赖。您需要更改业务逻辑以避免infra_assets_master上的查询。你究竟是怎么做的就是你的问题,因为你的业务逻辑不是我们的,但实质上你需要找到一种方法来填充历史表,其中只包含被删除的表中的信息。备选方案将所有SUBNET_LOOKUP_DELETE_TRIGGER逻辑放在从INFRA_MASTER_DELETE_TRIGGER调用的存储过程中。