我正在尝试对具有以下更新触发器的表执行更新,但是我收到以下错误:
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,<,< =,>,> =或当子查询用作 表达。
这是触发器......
如果photoRatingID的值设置为NULL,则会删除另一个表中的记录
IF UPDATE(photoRatingID)
BEGIN
IF (SELECT photoRatingID FROM inserted) IS NULL
BEGIN
DELETE mbr_Media_Approval_Primary
FROM deleted, mbr_Media_Approval_Primary
WHERE deleted.mbrID = mbr_Media_Approval_Primary.mbrID
END
END
所有表格都包含唯一ID' mbrID'
答案 0 :(得分:1)
您的SELECT photoRatingID FROM inserted
会返回多行,如错误消息所示。因此IS NULL
比较不起作用。您可以将或多或少的命令式方法转换为基于集合的方法,如下所示:
IF UPDATE(photoRatingID)
BEGIN
DELETE map
FROM mbr_Media_Approval_Primary map
JOIN deleted d ON d.mbrID=map.mbrID
JOIN inserted i ON i.mbrID=d.mbrID
WHERE i.photoRatingID IS NULL
END
答案 1 :(得分:0)
对于这种逻辑,您可以使用下面的CURSOR示例:
IF UPDATE(photoRatingID)
BEGIN
DECLARE @photoRatingID AS int;
DECLARE KURSOR_photoRating CURSOR FOR
SELECT photoRatingID FROM inserted;
OPEN KURSOR_photoRating;
FETCH NEXT FROM KURSOR_photoRating INTO @photoRatingID
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@photoRatingID IS NULL)
BEGIN
DELETE mbr_Media_Approval_Primary
FROM deleted, mbr_Media_Approval_Primary
WHERE deleted.mbrID = mbr_Media_Approval_Primary.mbrID
END
FETCH NEXT FROM KURSOR_photoRating INTO @photoRatingID
END
CLOSE KURSOR_photoRating;
DEALLOCATE KURSOR_photoRating;
END