其实有很多方法可以找了其所有下级,也不一定要使用递归,当然递归也是一种很好的办法。下面的方法就可以把你上述的结构转换成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

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/1343/1343007.xml?temp=.4894373
      

  2.   

    那只要在你的表进行修改或删除时,只做删除标记,对于修改则以修改后的数据插入到表,使其拥有新的ID。而在你保存数据时只管保存当时的ID,以后需要查询是就不会有问题。