3

CustomerContact如果SELECT语句返回大于 0,我希望对插入到我的表中的行进行修改(设置已删除 = 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是一个自动递增的主键列。

4

3 回答 3

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.
于 2013-10-16T10:14:26.403 回答
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

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

于 2013-10-16T10:29:08.107 回答
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
于 2014-01-13T14:31:59.367 回答