查找父类下的所有子类
create function f_child(@pid int)
returns @re table(id int,l int)
as
begin
declare @l int
set @l=0
insert @re select id,@l from category where parentid=@pid
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from category a join @re b on a.parentid=b.id
where b.l=@l-1
end
return
end
returns @re table(id int,l int)
as
begin
declare @l int
set @l=0
insert @re select id,@l from category where parentid=@pid
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from category a join @re b on a.parentid=b.id
where b.l=@l-1
end
return
end
/*调用 查找父类为1 的所有子类*/
select id from f_child(1)
select id from f_child(1)