其实有很多方法可以找了其所有下级,也不一定要使用递归,当然递归也是一种很好的办法。下面的方法就可以把你上述的结构转换成0000100001这各树型结构,这两种结构各有优缺点,这种树的最大好处就是查子类特别方便,但是维护复杂一些,不过你现在只要维护你现在的结构,就能在两种方式上选择方便的方法来实现你的需求,
drop table #temp
select id,fatherid,name,cast('' as varchar(8000)) as OrderCol into #temp from temp$declare @I int,@J int
select @I=1
select @j=(select max(id) from #temp)
while @I<=@j
begin
update #temp
set OrderCol=
isnull((select OrderCol from #temp as cc where id=#temp.FatherId),'')+
right('0000'+cast(#temp.fatherid as varchar),4)
+right('0000'+cast(#temp.id as varchar),4)
where #temp.id=@I
select @i=@I+1
end
select * from #temp order by OrderCol
drop table #temp
select id,fatherid,name,cast('' as varchar(8000)) as OrderCol into #temp from temp$declare @I int,@J int
select @I=1
select @j=(select max(id) from #temp)
while @I<=@j
begin
update #temp
set OrderCol=
isnull((select OrderCol from #temp as cc where id=#temp.FatherId),'')+
right('0000'+cast(#temp.fatherid as varchar),4)
+right('0000'+cast(#temp.id as varchar),4)
where #temp.id=@I
select @i=@I+1
end
select * from #temp order by OrderCol
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货