Home > Database > Mysql Tutorial > body text

ORA-04091:触发器/函数不能读

WBOY
Release: 2016-06-07 17:27:04
Original
1613 people have browsed it

关于自治事务解决触发器导致ORA-04091:触发器/函数不能读它不可行的验证。

关于自治事务解决触发器导致"ORA-04091:触发器/函数不能读它"不可行的验证。

经常会有人提出这样的问题:“有一个这样的问题,有一个表A有 a,b,c,d四个字段,修改一条记录d的值为2的倍数,希望把该记录插入相同结构的表B中。并删除A表的这条记录。”

相关SQL:

--一张表

create table FOO
(
  A NUMBER(10),
  B NUMBER(10),
  C NUMBER(10),
  D NUMBER(10)
)
;

--基于这张表的触发器

CREATE OR REPLACE TRIGGER tri_foo
AFTER INSERT OR UPDATE ON foo
FOR EACH ROW
DECLARE
  N_NUM NUMBER(5);
BEGIN
  IF MOD(TRUNC(:NEW.D),2) =0 THEN
      DELETE FROM FOO WHERE FOO.D = :NEW.D;
  END IF;
END;

这个函数在执行的时候会报告这样的错误:“ORA-04091:表SCOTT.FOO 发生了变化,触发器/函数不能读它”。网上有很多关于该错误的解决方案,其中,,有很大一部分是增加PRAGMA AUTONOMOUS_TRANSACTION语句,设置该触发器为自治事务,然后避免该错误。增加之后确实不报错了,而且貌似可以执行了,但是事真的如此么?修改之后的触发器如下:

CREATE OR REPLACE TRIGGER tri_foo
AFTER INSERT OR UPDATE ON foo
FOR EACH ROW
DECLARE
 PRAGMA AUTONOMOUS_TRANSACTION;
  N_NUM NUMBER(5);
BEGIN
  IF MOD(TRUNC(:NEW.D),2) =0 THEN
      DELETE FROM FOO WHERE FOO.D = :NEW.D;
  END IF;
  COMMIT;
END;

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!