我有如下表:我如何误解TSQL中的触发器递归?
if object_id(N'dbo.Node') is null
create table dbo.Node
(
ID bigint identity primary key,
ParentID bigint null, -- references Node(ID)
DateCreated datetime not null,
LastUpdated datetime not null,
[Name] nvarchar(500) not null,
);
我有这样的触发器来实现级联删除同一表中:
create trigger Node_Delete on Node for delete
as
begin
delete from Node where ParentID in (select id from deleted)
end
这里是我的数据集:
ID ParentID DateCreated LastUpdated Name
534 514 2010-01-12 10:15:03.940 2010-01-12 10:15:03.940 Test 1
535 534 2010-01-12 10:15:08.563 2010-01-12 10:15:08.563 Test 2
536 535 2010-01-12 10:15:12.063 2010-01-12 10:15:12.063 Test 3
537 536 2010-01-12 10:15:18.510 2010-01-12 10:15:18.510 Test 4
现在我执行此查询:
delete from Node where ID=534
这是得到的数据集:
ID ParentID DateCreated LastUpdated Name
536 535 2010-01-12 10:15:12.063 2010-01-12 10:15:12.063 Test 3
537 536 2010-01-12 10:15:18.510 2010-01-12 10:15:18.510 Test 4
为什么在触发DELETE语句不会导致触发的递归执行直到所有后代记录被删除?
编辑:请注意,我发布了下面的工作解决方案,但标记了与正确不同的答案,因为我的问题不是“解决方案是什么”,而是“为什么我会这样做不行”。
您不能在子 - 父关系上添加级联删除,因为SQL Server会抱怨多个级联路径。请注意,我更新了上面的问题。我不明白为什么触发器不会因删除而自行关闭。 – 2010-01-12 10:33:27
哦对。忘了那个。那么你将不得不使用CTE:http://msdn.microsoft.com/en-us/library/ms190766.aspx – 2010-01-12 10:36:12
我可以查看CTE,但我仍然想知道为什么我不能用触发器... – 2010-01-12 10:37:57