create proc p_delete
@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
insert 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'
go
select * from t1
go
delete from t1 where pid = 1
while @@rowcount>0
delete from t1 where pid not in (select id from t1 ) and pid <> 0