【问题标题】:SQL Error: ORA-04091: table is mutating while updating by a triggerSQL 错误:ORA-04091:表在由触发器更新时发生变异
【发布时间】:2016-01-11 16:08:11
【问题描述】:

我是甲骨文的新手,我正在开发一个医院管理系统,我有这张表来存储病人:

   create table patients(
p_id number not null primary key,
p_fullname full_name_ty,
p_gender char,
de_no number,
p_entry date ,
Diagnosis varchar2(25),

p_exit date,
constraint pdf foreign key (de_no) references department(dep_no)
);

其中 p_entry 是患者进入医院的日期,我创建了一个触发器,计算患者在 (p_exit) 日期更新后的住院时间(设置此日期意味着患者已经离开医院),触发器将简单地计算两个日期之间的差异,并打印出来,这里是触发器的代码:

create or replace
trigger period_trig before  update of p_exit on patients for each row
DECLARE             
period Number(3);
enterr DATE;
exitt DATE;
BEGIN
enterr := :old.P_ENTRY;
exitt:= :NEW.P_EXIT;
Period :=exitt-enterr; 
DBMS_OUTPUT.PUT_LINE('Duration:'||period);
update patients SET RESIDENCY= Period where P_ID = :old.P_ID;
end period_trig

当我测试触发器并使用如下更新语句时放置:

update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2;

运行它我得到这个错误:

Error starting at line 1 in command:
update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2
Error report:
SQL Error: ORA-04091: table SEM.PATIENTS is mutating, trigger/function may not see it
ORA-06512: at "SEM.UPDATEPAT", line 5
ORA-06512: at "SEM.PERIOD_TRIG", line 10
ORA-04088: error during execution of trigger 'SEM.PERIOD_TRIG'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.
Error starting at line 1 in command:
update patients set p_exit = to_date('01/02/2001','dd/mm/yyyy') where p_id = 2
Error report:
SQL Error: ORA-04091: table SEM.PATIENTS is mutating, trigger/function may not see it
ORA-06512: at "SEM.PERIOD_TRIG", line 11
ORA-04088: error during execution of trigger 'SEM.PERIOD_TRIG'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.

谁能告诉我如何解决它?非常感谢..

【问题讨论】:

标签: oracle triggers mutation


【解决方案1】:

您正在修改当前正在修改的触发器中的同一个表。正如他们的错误告诉你的那样:

*Cause:    A trigger (or a user defined plsql function that is referenced in
           this statement) attempted to look at (or modify) a table that was
           in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.

其实不用再更新表了,直接在表上使用虚拟列就可以了,这样整个触发器就变得多余了:

CREATE TABLE patients(
   p_id NUMBER NOT NULL PRIMARY KEY,
   p_fullname VARCHAR2(255),
   p_gender CHAR(1),
   de_no NUMBER,
   p_entry DATE,
   Diagnosis VARCHAR2(25),
   p_exit DATE,
   RESIDENCY NUMBER GENERATED ALWAYS AS (p_exit-p_entry)
);


insert into patients (p_id, p_fullname, p_gender, de_no, p_entry, diagnosis) values (1, 'GVENZL', 'M', 1234, SYSDATE-1, 'healthy' );
commit;

select p_fullname, residency from patients;

update patients set p_exit = sysdate;
commit;

select p_fullname, residency from patients;

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 2018-11-30
    • 2017-12-15
    • 2019-09-16
    • 2013-05-13
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多