SQL错误(1442),任何解决方案?

时间:2015-10-28 20:23:42

标签: mysql triggers mysql-error-1442

我在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)无法更新已存储的表'product'   函数/触发器,因为它已被调用的语句使用   这个存储的函数/触发器。 * /

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

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

但是,我又遇到了这个错误。

我真的需要在孩子价格变动时更新父母价格,你有什么解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:0)

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