无限级分类的删除
orderid parentid
1 0
2 1
3 1
4 2
0代表顶级,假如我要删除orderid为1的记录.就要把他的所有下级都删除
Create Table Test(orderId int,Parentid int)
insert into test
select 1,0 union select 2,1
union select 3,1 union select 4,2
------------------------------------------------------
Create Procedure kk
@i int
as
Declare @k int
Declare kc cursor local for
select orderid from test where parentid=@i
open kc
delete test from test where orderid=@i
fetch next from kc into @k
while @@fetch_status=0
begin
exec kk @k
fetch next from kc into @k
end
close kc
deallocate kc
------------------------end----------------------------
exec kk 1 ------執行過程就可刪除