我正在使用sqlserver,当我从Xtable删除重复行时,我创建了从YTable中删除的Trigger。 注意:YTable有一个来自XTable的外键为On_ID
触发查询
CREATE TRIGGER trgAfterDeletev4 ON XTable
AFTER DELETE
AS
declare @empid int;
select @empid=d.[On_ID] from deleted d;
Delete from YTable
where [On_ID]= @empid
PRINT 'AFTER DELETE TRIGGER fired.'
从XTable中删除重复的行
WITH C AS(
SELECT *,
RN = ROW_NUMBER()OVER(PARTITION BY [col1] ORDER BY [col2])
FROM [Sourcing].[src].XTable
)
DELETE FROM C WHERE RN > 1
结果
(0行(s)受影响)AFTER DELETE TRIGGER被解雇。
(1271060行(s)受影响)
请问这些查询的错误
非常感谢
答案 0 :(得分:3)
声明
select @empid=d.[On_ID] from deleted d
当您从XTable
删除多条记录时,将无法正常工作,因为您无法将多个ID放入单个整数变量中。
您只需将触发器中的删除语句更改为
即可 Delete from YTable
where [On_ID] in (select [On_ID] from deleted)