http://blog.csdn.net/playyuer/archive/2003/03/06/2854.aspx

解决方案 »

  1.   

    數據是不是有點問題? 
    id 為8 的好象沒有在depart 和dept 中出現過.
      

  2.   

    就是通过子类的ID选择所有相关的父类ID!!
      

  3.   

    他可以根据parent的值判断它的上一层是属于哪个id的?
    有谁能够解决这到难题啊??
      

  4.   

    仅供参考
    #a 为原数据表
    select id,convert(varchar(20),parent) as allid,convert(varchar(50),name) as allname
    into #b
    from #a where parent=0while @@rowcount>0
     begin
       update b set b.id=a.id,b.allid=b.allid+','+convert(varchar(2),a.parent),
         b.allname=b.allname+','+a.name
       from #b b,#a a where b.id=a.parent
       CONTINUE
     endselect b.id,right(allid,len(allid)-2),replace(allname,','+a.name,'')
    from #b b,#a a where b.id=a.id