有人可以帮我如何使用存储过程在其他表中添加字段

时间:2018-08-30 09:19:42

标签: sql-server

CREATE PROCEDURE addreturnitem
AS
    UPDATE item_inventory
    SET Stocks = (Stocks + Quantity) 
    FROM item_inventory 
    INNER JOIN Item_return ON item_inventory.Item_id = Item_return.Item_id

1 个答案:

答案 0 :(得分:0)

假设您正在Oracle DB中运行此查询。 您的查询问题是UPDATE子句中的JOIN条件。 Oracle在Update语句中不允许JOIN子句。

您可以按以下方式编辑查询:

CREATE PROCEDURE addreturnitem
AS
UPDATE item_inventory inv 
   SET stocks = (SELECT inv.stocks + ret.quantity
                   FROM Item_return ret
                  WHERE ret.item_id = inv.item_id)
WHERE EXISTS (SELECT 1
                FROM item_return ret2 
               WHERE ret2.item_id = inv.item_id);

此外,查询可以像下面这样进行编辑,这样更易​​于阅读:

CREATE PROCEDURE addreturnitem
AS
UPDATE 
(SELECT inv.stock as stock, ret.quantity as ret_quantity
   FROM item_inventory inv
  INNER JOIN item_return ret
     ON inv.item_id = ret.item_id
) upd
SET upd.stock = upd.stock + upd.ret_quantity;