我在插入后创建了一个更新列的触发器。我遇到的问题是IsNowComplete
CTE似乎没有处理多个"选择列表"有多个项目。我如何解释这种情况?我的触发器代码:
AFTER INSERT, UPDATE
AS
WITH WasComplete AS
(
SELECT
pl.IsComplete, pl.Id
FROM
dbo.PickList pl
JOIN
inserted i ON pl.Id = i.PickListId
), IsNowComplete AS
(
SELECT
i.PickListId,
IsNowComplete = CASE
WHEN COUNT(1) > 0 THEN 0 ELSE 1
END
FROM
dbo.PickListContent plc
JOIN
inserted i ON plc.PickListId = i.PickListId
WHERE
plc.PickListId = i.PickListId
AND plc.QuantityPicked != plc.QuantityNeeded
GROUP BY
i.PickListId
)
UPDATE pl
SET IsComplete = CASE
WHEN ws.IsComplete = 0 AND inc.IsNowComplete = 1
THEN 1
WHEN ws.IsComplete = 1 AND inc.IsNowComplete = 0
THEN 0
ELSE
ws.IsComplete
END
FROM dbo.PickList pl
JOIN WasComplete ws ON pl.Id = ws.Id
JOIN IsNowComplete inc ON ws.Id = inc.PickListId
END
答案 0 :(得分:0)
请将isNowComplete的WHERE子句修改为
AND plc.QuantityPicked不存在(从dbo.PickListContent plc1中选择plc1.QuantityNeeded)
如果没有空值,您也可以使用NOT IN。
希望这有帮助。