0

我在 MySQL 中创建了这个触发器(在表产品更新之后):

BEGIN
 set @parentid := (select parent_id from product where product_id = new.product_id);
 if (old.price <> new.price) then
  update product set price=new.price where product_id = @parentid and price > new.price;
 end if;
END

但是,当我更新价格时,我收到了这个错误

: SQL 错误 (1442) 无法更新存储函数/触发器中的表“产品”,因为它已被调用此存储函数/触发器的语句使用。*/

我创建了两个表和两个触发器,例如:

Trigger 1(表产品更新后):

BEGIN
 if (old.price <> new.price) then
    insert into product_price
    (product_id, price , date_added)
    values
    (new.product_id, new.price, SYSDATE());
 end if;
END

Trigger 2(在表上插入后product_price):

BEGIN
 SET @parentid := (select parent_id from product where product_id = new.product_id);
 if (@parentid >0) then
  update  product set price=new.price where product_id in (select @parentid);
 end if;
END

但是,我又得到了那个错误。

当孩子价格发生变化时,我真的需要更新父母价格,你有什么解决办法吗?

谢谢。

4

1 回答 1

0

您不能使用触发器,因为您需要更新正在更新的同一个表。您需要将此逻辑移动到应用程序代码中或使用存储过程而不是触发器。

于 2015-10-28T20:37:22.817 回答