使用表连接触发

时间:2016-04-12 08:26:51

标签: sql oracle

我想创建一个更新库存的触发器。我的客户订购的库存和数量的数量在两个不同的表上。我如何加入?

CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW 
BEGIN
 update product values 
  (:old.product_id,
    :old.product_name,
    :old.description,
    quantity_on_stock-s.quantity,
    :old.minimal_quantity,
    :old.unit_price,
    :old.product_type_id);
  from product, sale_order s
  where product.product_id=s.product_id;
END;

OR

CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW 
BEGIN
 update product set 
 p.quantity_on_stock= p.quantity_on_stock-s.quantity;
  from product p , sale_order s
  where p.product_id=s.product_id;  
END;

sale_order表包含sale_noquantityproduct_id列。

1 个答案:

答案 0 :(得分:1)

您无法加入更新声明。你的第二次尝试更接近,但仍在加入,并且没有相关性。你也有一个流浪的分号。我想你正在寻找类似的东西:

CREATE OR REPLACE
TRIGGER UPDATE_QUANTITY_TRIGGER
AFTER insert ON SALE_ORDER
FOR EACH ROW 
BEGIN
 update product p set 
   p.quantity_on_stock = p.quantity_on_stock - :new.quantity
  where p.product_id = :new.product_id;  
END;

这使用来自:new伪记录的值来识别要更新的产品记录,并获得减少库存水平的数量。无需再次加入触发表 - 无论如何都不允许这样做。

您可能会在多用户环境中遇到意外行为或结果进行此类更新。