--写个处理函数
create function f_id()
returns @re table(id int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select id,@l,right(10000+id,4)
from 表 where parentid=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l,b.sid+right(10000+a.id,4)
from 表 a,@re b
where a.parentid=b.id and b.[level]=@l-1
end
return
end
go
--调用函数实现排序
select a.*
from 表 a,f_id()b
where a.id=b.id
order by b.sid
go