我在一张表上应用了“删除后”触发器,下面是脚本:
ALTER TRIGGER [dbo].[onDelete_N_UR]
ON [dbo].[Notification_UnRead]
AFTER delete
AS
BEGIN
SET NOCOUNT ON;
declare @roid int
set @roid=(select ReachOutID from deleted(nolock)
where deleted.NotificaionType='reachoutlike')
update CACHE_Reachout
set CACHE_Reachout.LIKEcount=(select [dbo].[getReachout_Notification_Count](@roid,'like') )
where CACHE_Reachout.ReachOutID=@roid
结束
现在我尝试使用以下sql语句批量删除一些行:
delete from Notification_UnRead where Notification_ID=****
它给了我错误
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
当删除触发器应用到上面时,如何使用上面的删除语句删除多行。
最佳答案
试试这个 -
ALTER TRIGGER [dbo].[onDelete_N_UR]
ON [dbo].[Notification_UnRead]
AFTER DELETE
AS BEGIN
SET NOCOUNT ON;
DECLARE @roid INT
SET @roid =
(
SELECT TOP 1 ReachOutID
FROM DELETED d
WHERE d.NotificaionType = 'reachoutlike'
)
UPDATE CACHE_Reachout
SET CACHE_Reachout.LIKEcount = dbo.getReachout_Notification_Count(@roid, 'like')
WHERE CACHE_Reachout.ReachOutID = @roid
END
或者尝试这个(更适合使用)-
ALTER TRIGGER [dbo].[onDelete_N_UR]
ON [dbo].[Notification_UnRead]
AFTER DELETE
AS BEGIN
SET NOCOUNT ON;
UPDATE t
SET LIKEcount = dbo.getReachout_Notification_Count(d.ReachOutID, 'like')
FROM CACHE_Reachout t
JOIN DELETED d ON t.ReachOutID = d.ReachOutID
WHERE d.NotificaionType = 'reachoutlike'
END
关于sql-server - 通过 sql 语句 "delete from tableName"删除多行,而 TRIGGER "After Delete"已应用于该语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17856337/