oralce 触发器 ORA-04091:表XX发生了变化,触发器/函数不能读它

这句话的意思是 你在触发器/函数中使用了被修改的表

比如我的


create or replace trigger   A.sync_exam_item_branch_to_b
after insert or delete or update on A.Exam_item_branch
for each row
begin

错误用法
  if inserting then
        insert into b.tb00b10(id, item_id, dept_id, is_active)
        select
           t.id,
           (select v.id from b.v_tb00b00_vs_advicetype v where v.code = t.item_id and ROWNUM =1) item_id,
           (select v.id from b.tb00c00 v where v.code = t.BRANCH_ID) dept_id,
           t.STATE is_active --        ,t.*
        from  A.Exam_item_branch  t -- 注意这里,这个表不能被查询
  where t.id = :NEW.id;

  end if ;

正确用法
.......
  if inserting then
      insert into b.tb00b10(id, item_id, dept_id, is_active)
     select
           :new.id,
           (select v.id from b.v_tb00b00_vs_advicetype v where v.code = :new.item_id and ROWNUM =1) item_id,
           (select v.id from b.tb00c00 v where v.code = :new.BRANCH_ID) dept_id,
           :new.STATE is_active
        from   dual  -- 注意 表XX发生了变化,触发器/函数不能读它
           where not exists( select 1 from b.tb00b10 b10 where b10.id = :new.id);
  end if ;
......

end;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。