;with t as ( select id,Parent from tb where id=@id union all select a.id,a.Parent from a.id=b.Parent ) delete tb from tb,t where tb.id=t.id多级
--设置外键自引用,级联删除: ALTER TABLE tb ADD CONSTRAINT FK_tb_Parent FOREIGN KEY Parent REFERENCES tb(ID) ON DELETE CASCADE GO --删除ID = @ID的记录及其所有下属记录: delete from tb where ID = @ID GO
(
select id,Parent from tb where id=@id
union all
select a.id,a.Parent from a.id=b.Parent
)
delete tb from tb,t where tb.id=t.id多级
--设置外键自引用,级联删除:
ALTER TABLE tb ADD CONSTRAINT FK_tb_Parent FOREIGN KEY Parent REFERENCES tb(ID) ON DELETE CASCADE
GO
--删除ID = @ID的记录及其所有下属记录:
delete from tb where ID = @ID
GO