仅供参考 #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
id 為8 的好象沒有在depart 和dept 中出現過.
有谁能够解决这到难题啊??
#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