首先删除pid = 1
然后使用循环删除,PID不在ID里面的(PID=0的除外),行不行?
delete t1insert into t1
select 1, 0, 'sdf'
union
select 2, 1, 'sdf'
union
select 3, 1, 'sdf'
union
select 4, 2, 'sdf'
union
select 5, 2, 'sdf'
union
select 6, 3, 'asdf'
union
select 7, 3, 'asdf'
goselect * from t1
godelete from t1 where pid = 1
while @@rowcount>0
delete from t1 where pid not in (select id from t1 ) and pid <> 0
然后使用循环删除,PID不在ID里面的(PID=0的除外),行不行?
delete t1insert into t1
select 1, 0, 'sdf'
union
select 2, 1, 'sdf'
union
select 3, 1, 'sdf'
union
select 4, 2, 'sdf'
union
select 5, 2, 'sdf'
union
select 6, 3, 'asdf'
union
select 7, 3, 'asdf'
goselect * from t1
godelete from t1 where pid = 1
while @@rowcount>0
delete from t1 where pid not in (select id from t1 ) and pid <> 0
CREATE TRIGGER tr_delete ON dbo.t1
FOR DELETE
AS
delete from t1 where Parent_id in (select id from deleted)
@id int --要删除的id
as
create table #t(id int,level int)
declare @l int
set @l=0
insert #t select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert #t select a.id,@l
from 表 a join #t b on a.Parent_id=b.id
where b.level=@l-1
end
delete a
from 表 a join #t b on a.id=b.id
go--调用
exec p_delete 2