这是数据库结构,主要体现父类子类的关系!!
   id         name      parent      depth
    1        aa             0        1
    2        bb             1        2
    3        cc             2        3
    4        dd             3        4
    5        ee             0        1
    6        ff             5        2
    7        gg             6        3
    8        hh             7        4
这是另外一个表
   id     name      content
   1      AAA2        AAA3
   5      AAA         AAA1
   6       BBB         BBB1
想通过视图使之显示
  id     name      content
   5      AAA         AAA1
   6      AAA         AAA1
   7      AAA         AAA1
   8      AAA         AAA1
   1      AAA2        AAA3
   2      AAA2        AAA3
   3      AAA2        AAA3
   4      AAA2        AAA3 
   6      BBB         BBB1
   7      BBB         BBB1
   8      BBB         BBB1
当知道子类获取父类的大致算法如下
create function f_pid2()
returns @re table(id int,level int,gid int)
as
begin
declare @l int
set @l=0
insert @re select id,@l,id from View_AA
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.parent,@l,b.gid 
from Product_Class a,@re b
where a.Product_Class_id=b.id
and b.level=@l-1
and a.parent<>0
end
return
end
有谁可以帮我把它改成知道父类获取子类的大致算法??