给你一个类似的参考:
树排序:
ContentId   ContentSort OrderId     AllOrderId  
----------- ----------- ----------- ----------- 
1           0           0           0
2           0           1           0
21          1           2           0
22          1           0           0
23          1           1           0
24          21          0           0
25          22          1           0
26          23          2           0
30          21          1           0select 0  ContentId, identity(int,1,1) id into #t 
declare @orderid int
declare @cid int
set @orderid = 1
while exists (select * from ContentTable where AllOrderId = 0)
begin
  select top 1 @cid = ContentId from #t order by id desc
  if exists (select ContentId from ContentTable where ContentSort = @cid and AllOrderId = 0)
  begin
     select top 1 @cid = ContentId from ContentTable where ContentSort = @cid and AllOrderId = 0 order by orderid
     insert #t (contentid) values (@cid)
     update ContentTable set Allorderid = @orderid where contentid = @cid
     set @orderid = @orderid + 1
  end
  else
  begin
    delete #t where contentid = @cid
  end
end
declare @s varchar(1000)
set @s = ''
select @s = @s + rtrim(cast(contentid as char))+ '-' from ContentTable order by AllOrderId
select left(@s, len(@s)-1)
drop table #t