sqlserver递归实现部门树形结构排序
WITH T1
AS
(select *, ROW_NUMBER() over (partition by fid order by bid DESC) as Rn FROM test2
),
T2
AS
(select fid,bid, name,cast ( Rn as varchar (10)) as Rn FROM T1 where fid =0
union all
select T1.fid,T1.bid, T1.name, cast (cast(T2.Rn as varchar (10)) + '.' + cast ( T1.Rn as varchar(10)) as varchar(10) )FROM T1 join T2 on T1.fid = T2.bid
)
select*from T2