SQL 基于比较删除值和插入值触发操作

SQL Trigger action based off comparing deleted and inserted value

我用 C# 编写了一个库存应用程序,想跟踪库存数量的变化。为此,我有两个 table:tblInventory 和 tblInvChange。目前,我有一个 AFTER UPDATE 触发器,它通过将以下内容添加到 tblInvChange 来很好地工作:(使用 d. 表示已删除,i. 表示已插入)d.lastphysical; i.lastphysical; d.quantity; i.quantity.

我的问题是,如果我在同一天返回并更改某件商品的数量,我将在同一日期获得同一件商品的两条记录。如果日期不存在,我想让触发器在 tblInvChange table 中插入一条记录,如果存在则更新当前记录。

    BEGIN
If(Select [fldlastPhysical] from deleted) <> (Select [fldLastPhysical] from inserted)

INSERT tblInvTracking(keyProductID, fldLocationId, fldLastPhysical, fldQuantity, fldInventoryChange, fldNewQuantity)
SELECT
   D.keyProductID, D.fldLocationID, D.fldLastPhysical, d.fldQuantity, i.fldLastPhysical, i.fldQuantity
FROM
   DELETED D JOIN INSERTED I ON D.keyProductID = I.keyProductID AND D.fldLocationID = I.fldLocationID;

Else

UPDATE tblInvTracking(keyProductID, fldLocationId, fldLastPhysical, fldQuantity, fldInventoryChange, fldNewQuantity)
SELECT
   D.keyProductID, D.fldLocationID, D.fldLastPhysical, d.fldQuantity, i.fldLastPhysical, i.fldQuantity
FROM
   DELETED D JOIN INSERTED I ON D.keyProductID = I.keyProductID AND D.fldLocationID = I.fldLocationID;

END

这是我的理解,但行不通。将不胜感激有关正确方法的示例。

您需要创建 2 个语句,一个插入不存在的记录,一个更新存在的记录。据我所知,它会像下面这样,但这不是 100% 正确的,您需要仔细研究逻辑并确保它与您要实现的目标相匹配。

注意:这是假设 fldLastPhysical 是感兴趣的日期,并且它是 date 而不是 datetime

-- WHERE THE RECORD DOESN'T EXIST FOR THE GIVEN DATE - ADD A NEW ONE
INSERT tblInvTracking(keyProductID, fldLocationId, fldLastPhysical, fldQuantity, fldInventoryChange, fldNewQuantity)
    SELECT D.keyProductID, D.fldLocationID, D.fldLastPhysical, d.fldQuantity, i.fldLastPhysical, i.fldQuantity
    FROM DELETED D
    JOIN INSERTED I ON D.keyProductID = I.keyProductID AND D.fldLocationID = I.fldLocationID
    -- ONLY ADD A NEW RECORD FOR THIS DATE IF IT DOESN"T ALREADY EXIST
    WHERE NOT EXISTS (
      SELECT 1
      FROM tblInvTracking T
      WHERE T.fldlastPhysical = D.fldLastPhysical
      AND T.keyProductID = D.keyProductID AND T.fldLocationID = D.fldLocationID
    );

-- WHERE THE RECORD EXISTS FOR THE GIVEN DATE - UPDATE EXISTING
UPDATE T SET
    fldQuantity = T.fldQuantity + X.fldQuantity
    -- It appears the following line is a datetime column, so you will need to determine what logic is required there - maybe just a straight update.
    , fldInventoryChange = X.fldInventoryChange
    , fldNewQuantity = T.fldNewQuantity + X.fldNewQuantity
FROM tblInvTracking T
INNER JOIN (
    SELECT D.keyProductID, D.fldLocationID, D.fldLastPhysical, d.fldQuantity, i.fldLastPhysical fldInventoryChange, i.fldQuantity fldNewQuantity
    FROM DELETED D
    JOIN INSERTED I ON D.keyProductID = I.keyProductID AND D.fldLocationID = I.fldLocationID
    -- ONLY ADD A NEW RECORD FOR THIS DATE IF IT DOESN"T ALREADY EXIST
    WHERE EXISTS (
      SELECT 1
      FROM tblInvTracking T
      WHERE T.fldlastPhysical = D.fldLastPhysical
      AND T.keyProductID = D.keyProductID AND T.fldLocationID = D.fldLocationID
    )
) X ON T.fldlastPhysical = X.fldLastPhysical AND T.keyProductID = X.keyProductID AND T.fldLocationID = X.fldLocationID