查找父类下的所有子类

 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


/*调用 查找父类为1 的所有子类*/
select id from f_child(1)

posted @ 2007-04-23 15:19  M'  阅读(1109)  评论(0编辑  收藏  举报