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
是一个自动递增的主键列。