我正在建立库存控制系统,我在更新余额方面遇到了问题。问题是,如果我以不同的价格购买相同的商品,那只是更新第一行的余额。
见下面给出的行:
itemcode item quantity price
------------------------------
101 CPU 10 6000/-
101 CPU 15 6500/-
假设我在datatable中传递这些值,然后将这些值作为参数传递给存储过程,在存储过程中我将在下面的给定行中写入:
IF EXISTS (SELECT t.itemno, t.department
FROM @itemupdate AS t
INNER JOIN item_detail p ON t.itemno = p.itemno
AND t.department = p.department)
BEGIN
--update existing record
UPDATE item_detail
SET purqt = item_detail.purqt + t.purqt
FROM @itemupdate AS t
WHERE t.itemno = item_detail.itemno
AND t.department = item_detail.department
END
ELSE
BEGIN
--insert new record
INSERT INTO item_detail
SELECT
t.itemno, t.itemname, t.purqt, t.unit, t.department
FROM
@itemupdate t
END