SQL error - trigger/function may not see it

老子叫甜甜 提交于 2021-02-11 12:20:13

问题


I was working on my project, here I got this error while inserting some values in a row:

ERROR at line 1: ORA-04091: table SYSTEM.PRODUCTS is mutating, trigger/function may not see it ORA-06512: at "SYSTEM.PROD_TOTAL", line 2 ORA-04088: error during execution of trigger 'SYSTEM.PROD_TOTAL'

This is my insert statement:

insert into products 
values (1, 1001, 'Medical', 20, 4, 1, 1, 1);

Products table :

create table Products
(
    ProdId number primary key,
    ProdNum number not null unique,
    ProdType varchar2(15),
    ProdPrice int,
    ProdQuantity int,
    ProdCustId int references Customers,
    ProdOrdId int references Orders,
    ProdStoreId int references Stores
);

Trigger code:

create trigger PROD_TOTAL
after insert ON Products
for each row
begin
    update Payments
    set ProdTotal = (select Products.ProdPrice * Products.ProdQuantity from Products);
end;
/

And finally my Payment table:

create table Payments
(
    PayId int primary key,
    PayDate date,
    ProdTotal int,
    FinalTotal int,
    PayOrdId int references orders,
    PayProdId int references Products,
    PayCustId int references Customers
);

I don't know why I am getting this error, please help me in solving this issue...


回答1:


A statement level trigger (i.e. without FOR EACH ROW clause) will update always all records in Payments table, I don't think that's needed. For an update of only related products, use this trigger:

create trigger PROD_TOTAL
after insert ON Products
for each row
begin
    update Payments
    set ProdTotal = :new.ProdPrice * :new.ProdQuantity
    WHERE PayProdId = :new.ProdId ;
end;


来源:https://stackoverflow.com/questions/65857565/sql-error-trigger-function-may-not-see-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!