假设我有一个表 MyTab:
MyTab(ID, Col1, Col2, Col3,...)
然后我创建了一个删除触发器,如:
CREATE TRIGGER [dbo].[trg_MyTab_Delete]
ON [dbo].[MyTab]
For DELETE AS
begin
INSERT INTO dbo.DeleteHistory(...)
SELECT * FROM deleted d;
end
这适用于单行数据删除。但它不适用于批量删除,例如
delete from MyTab where ...
如果上面删除删除 10 行MyTab
,触发器将插入 10*10 = 100 行DeleteHistory
。
如何解决此问题以使此触发器适用于单行和多行删除?