我有两张表,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.