MSSQL触发器 - 在INSERT上更新新插入的记录

时间:2013-10-16 10:12:23

标签: sql-server triggers sql-update sql-insert last-insert-id

如果CustomerContact语句返回的值超过0,我希望对插入到我的表SELECT中的行进行修改(Set Deleted = 1)。

我有以下内容,但仍未经测试:

CREATE TRIGGER mark_cust_contact_deleted ON CustomerContact
AFTER INSERT AS
BEGIN
DECLARE @numrows INT;

    /* Determine if order matches criteria for marking customer contact as DELETED immediately */
    SELECT @numrows = COUNT(*)
    FROM [Order] o
    JOIN OrderMeterDetail om
          ON o.OrderID = om.OrderID
    WHERE o.WorkTypeID = 3 AND o.WorkActionID = 26 AND o.WorkStageID IN (109, 309, 409)
    AND om.MeterDetailTypeID = 1 AND om.MeterLocationID IN (2, 4)
    AND o.orderid IN (SELECT OrderID FROM INSERTED);

    /* If the order matches the criteria, mark the customer contact as deleted */
    IF (@numrows >= 1)

        UPDATE CustomerContact
        SET Deleted = 1
        WHERE CustomerContactID IN (SELECT CustomerContactID FROM INSERTED);

END

在我的IF语句中,我正在使用FROM INSERTED,假设这将为插入创建的记录返回新插入的ID

我对这个陈述有两个问题:

  • 声明的这一部分是否只会记录UPDATE 刚插入CustomerContact

    UPDATE CustomerContact SET Deleted = 1 WHERE CustomerContactID IN (SELECT CustomerContactID FROM INSERTED);

  • 这是根据SELECT声明的结果更改刚刚插入的行的方式吗?

CustomerContactID是一个自动递增的主键列。

3 个答案:

答案 0 :(得分:2)

你说“只是插入的记录”。 Inserted可以包含多条记录。如果只有一个,那么你的触发器将按预期运行。但如果不止一个,它就不会。

我会将你的逻辑重写为一行update语句......

 Update CustomerContact
 Set Deleted = 1
 From CustomerContact
       inner join inserted on CustomerContact.CustomerContactID = inserted.CustomerContactID
       inner join orders on inserted.OrderID = orders.OrderID
 where
     -- some criteria.

答案 1 :(得分:1)

CREATE TRIGGER mark_cust_contact_deleted ON CustomerContact
AFTER INSERT AS
BEGIN
DECLARE @numrows INT;

    /* Determine if order matches criteria for marking customer contact as DELETED immediately */
    -- Get all the records into a temp table 
    SELECT * INTO #Temp
    FROM inserted
    Declare @ID int;

    SELECT @numrows = COUNT(*)
    FROM [Order] o
    JOIN OrderMeterDetail om
          ON o.OrderID = om.OrderID
    WHERE o.WorkTypeID = 3 AND o.WorkActionID = 26 AND o.WorkStageID IN (109, 309, 409)
    AND om.MeterDetailTypeID = 1 AND om.MeterLocationID IN (2, 4)
    AND o.orderid IN (SELECT OrderID FROM #Temp);
 IF (@numrows >= 1)
  BEGIN
    WHILE EXISTS (SELECT TOP 1 * FROM #Temp)
    BEGIN

    SELECT TOP 1 @ID = ID FROM #Temp


    /* If the order matches the criteria, mark the customer contact as deleted */


        UPDATE CustomerContact
        SET Deleted = 1
        WHERE CustomerContactID IN (SELECT CustomerContactID FROM #Temp WHERE ID = @ID);

        DELETE FROM #Temp WHERE ID = @ID
    END
  END   
     DROP TABLE #Temp
END

我认为你可以做这样的事情,调整代码以进一步满足需求,希望这会有所帮助。

答案 2 :(得分:0)

以下是我用来解决此问题的最终解决方案:

CREATE TRIGGER mark_cust_contact_deleted ON CustomerContact
AFTER INSERT AS
BEGIN

    UPDATE CustomerContact
    SET Deleted = 1
    FROM CustomerContact cc
        JOIN inserted i
            ON cc.CustomerContactID = i.CustomerContactID
        JOIN [Order] o
            ON i.OrderID = o.OrderID
        JOIN OrderMeterDetail om
            ON i.OrderID = om.OrderID
    WHERE o.WorkTypeID = 3 AND o.WorkActionID = 26 AND o.WorkStageID IN (109, 309, 409)
    AND om.MeterDetailTypeID = 1 AND om.MeterLocationID IN (2, 4)

END