1

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

2 回答 2

0

在行触发器中,没有 SQL 语句可以访问触发器所在的表。您SELECT AVG(W_PRICE) FROM STATE_PRICE WHERE PRODUCTID = W_PRODUCT是导致错误的原因。解决此限制的经典方法是使用复合触发器 - 文档here。另请参阅我对这个 StackOverflow 问题的回答,了解实现复合触发器的示例。

分享和享受。

于 2013-04-18T23:21:15.027 回答
0

可能存在引用完整性约束(在 productid 上),这也可能引发相同的错误。如果是这种情况,下面的链接可以帮助您避免错误。

http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551198119097816936

于 2013-04-18T21:30:41.877 回答