我有兩個表,PRODUCTS和STATE_PRICE。每種產品的價格因州而異。 PRODUCTS表追蹤所有州的每種產品的平均成本。我試圖編寫一個觸發器,在STATE_PRICE表中插入,更新或刪除價格時,將更新PRODUCTS表中項目的平均價格。我編寫了以下編譯的觸發器,但是當我測試它時,我收到了一條突變的錯誤消息。我理解變異錯誤的概念,我試圖更新一個觸發器正在執行的表,但我實際上是在STATE_PRICE表上執行觸發器時嘗試更新PRODUCTS表。觸發器上的突變錯誤以計算平均成本
create or replace trigger trg_avg_cost
after insert or update or delete on state_price
for each row
declare
w_price state_price.list_price%type;
w_product state_price.productid%type;
begin
w_price := :new.list_price;
w_product := :new.productid;
update products
set avg_cost_per_unit = (select avg(w_price) from state_price
where productid = w_product);
end;
/
特定錯誤消息我得到說:
錯誤報告:
SQL Error: ORA-04091: table STATE_PRICE is mutating, trigger/function may not see it
ORA-06512: at "TRG_AVG_COST", line 9
ORA-04088: error during execution of trigger 'TRG_AVG_COST'
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.
我認爲您必須在聲明中使用Pragma Autonomous_transaction並再次檢查 – ajmalmhd04