ORA-04091: table 正在变异,trigger/function 可能看不到它,ORA-06512:, ORA-06512: 在 "SYS.DBMS_SQL",第 1721 行

ORA-04091: table is mutating, trigger/function may not see it, ORA-06512:, ORA-06512: at "SYS.DBMS_SQL", line 1721

问题:创建一个名为 trg_job_date 的触发器,它将作业 table 中的 JOB_LAST_UPDATE 列更新为插入或更新时的当前日期。使用插入和更新语句测试触发器 – 包括插入和更新语句。

所以我正在尝试通过触发器更新作业中的 job_last_update,但我读到我可以在触发器中更新相同的 table。我的主要问题是 job_last_update 仅在作业 table.

中可用

这是 erd:

到目前为止,这是我的代码:

create or replace trigger trg_job_date 
after insert or update
on job
for each row 

begin
    update job
        set job_last_update = current_date;
end;

insert into job (job_code, job_description, job_chg_hour, job_last_update)
values('511', 'Hardware management', '22.11', '4/4/2021');

update job 
set job_description = 'Hardware Manager' 
where job_code = 511;

select * from job; --using this to test whether the trigger worked properly

触发器 运行 没问题,但是当我尝试 运行 我的插入语句时它给我错误:

ORA-04091: table SQL_IUARPVSJLXIJWEVKOWRJRCSXZ.JOB is mutating, trigger/function may not see it ORA-06512: at "SQL_IUARPVSJLXIJWEVKOWRJRCSXZ.TRG_JOB_DATE", line 2
ORA-06512: at "SYS.DBMS_SQL", line 1721

我需要做什么才能使我的代码执行我需要它执行的操作?

不要创建带有单独更新的新事务。在 before 触发器中使用 :new 伪 table 将列修改为同一事务的一部分。

create or replace trigger trg_job_date 
before insert or update
on job
for each row 
begin
    :new.job_last_update := sysdate;
end;

在此处查看文档:https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-triggers.html#GUID-4F93F21E-BA7F-4378-87E6-46A8E4C03287