Reputation: 492
I have applied "After Delete" trigger on one table, below is the script:
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
END
Now I am trying to delete some rows in bulk using following sql statement:
delete from Notification_UnRead where Notification_ID=****
And it's giving me error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
How can I delete multiple rows using above delete statement when delete trigger applied on it .
Upvotes: 2
Views: 2186
Reputation: 492
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[onDelete_N_UR]
ON [dbo].[Notification_UnRead]
AFTER delete
AS
BEGIN
SET NOCOUNT ON;
declare @roid int
CREATE TABLE #TempTable (ReachOutID INT)
INSERT INTO #TempTable (ReachOutID)
select ReachOutID from deleted(nolock) where deleted.NotificaionType='reachoutlike'
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR
SELECT ReachOutID
FROM #TempTable
OPEN @getAccountID
FETCH NEXT
FROM @getAccountID INTO @roid
WHILE @@FETCH_STATUS = 0
BEGIN
update CACHE_Reachout set CACHE_Reachout.LIKEcount=(select [dbo].[getReachout_Notification_Count](@roid,'like') ) where CACHE_Reachout.ReachOutID=@roid
FETCH NEXT
FROM @getAccountID INTO @roid
END
CLOSE @getAccountID
DEALLOCATE @getAccountID
drop table #TempTable
END
Upvotes: 0
Reputation: 492
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(nolock)
WHERE DELETED.NotificaionType = 'reachoutlike'
)
UPDATE CACHE_Reachout
SET CACHE_Reachout.LIKEcount =
(
SELECT [dbo].[getReachout_Notification_Count](@roid, 'like')
)
WHERE CACHE_Reachout.ReachOutID = @roid
END
Upvotes: 0
Reputation: 121922
Try this one -
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
Or try this (more preferably for using) -
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
Upvotes: 2
Reputation: 10843
Either
set @roid=(select ReachOutID from deleted(nolock)
where deleted.NotificaionType='reachoutlike')
Or
set CACHE_Reachout.LIKEcount=
(select [dbo].[getReachout_Notification_Count](@roid,'like') )
where CACHE_Reachout.ReachOutID=@roid
is returning more than 1 row of data.
Raj
Upvotes: 0