参考: /* 标题:SQL SERVER 2005中树结构按照各顶级节点排序 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间:2010-10-24 地点:陕西西安 说明: 如下树级结构中需要按照各顶级节点排序 id pid name ---- ---- ---------- 001 000 广东省 002 001 广州市 003 001 深圳市 004 002 天河区 005 003 罗湖区 006 003 福田区 007 003 宝安区 008 007 西乡镇 009 007 龙华镇 010 007 松岗镇 011 000 江西省 012 011 南昌市 013 012 南昌县 014 012 新建县 015 012 进贤县 016 012 安义县 017 013 麻丘镇 018 011 九江市 需要的结果: id name pid ---- ---------- ---- 001 广东省 000 002 广州市 001 004 天河区 002 003 深圳市 001 005 罗湖区 003 006 福田区 003 007 宝安区 003 008 西乡镇 007 009 龙华镇 007 010 松岗镇 007 011 江西省 000 012 南昌市 011 013 南昌县 012 017 麻丘镇 013 014 新建县 012 015 进贤县 012 016 安义县 012 018 九江市 011 */create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10)) insert into tb values('001' , '000' , N'广东省') insert into tb values('002' , '001' , N'广州市') insert into tb values('003' , '001' , N'深圳市') insert into tb values('004' , '002' , N'天河区') insert into tb values('005' , '003' , N'罗湖区') insert into tb values('006' , '003' , N'福田区') insert into tb values('007' , '003' , N'宝安区') insert into tb values('008' , '007' , N'西乡镇') insert into tb values('009' , '007' , N'龙华镇') insert into tb values('010' , '007' , N'松岗镇') insert into tb values('011' , '000' , N'江西省') insert into tb values('012' , '011' , N'南昌市') insert into tb values('013' , '012' , N'南昌县') insert into tb values('014' , '012' , N'新建县') insert into tb values('015' , '012' , N'进贤县') insert into tb values('016' , '012' , N'安义县') insert into tb values('017' , '013' , N'麻丘镇') insert into tb values('018' , '011' , N'九江市') go;with t as ( select px = cast(id as varchar(500)) , * from tb t where not exists(select 1 from tb where id = t.pid) union all select cast(b.px + ',' + a.id as varchar(500)) , a.* from tb a , t b where a.pid = b.id ) select id , name , pid from t order by pxdrop table tb
f not object_id('tempdb..#test') is null begin drop table #test end create table #test(DepartId int, DepartNamen nvarchar(36), ParentDepartId int, SortId int)insert into #test select 1,'技术部',0,1 union all select 2,'宣传部',0,2 union all select 3,'销售部',0,3 union all select 4,'网络部',0,4 union all select 5,'技术二部',1,5 union all select 6,'网络二部',4,6 union all select 7,'网络三部',4,7 union all select 8,'技术三部',1,8 union all select 9,'网络部1',6,9 union all select 10,'技术部1',5,10 union all select 11,'技术部2',8,11 select a.DepartId as f_id,a.DepartNamen as f_name,isnull(b.DepartNamen,'no children') as children_name from #test a left join #test b on a.DepartId=b.ParentDepartId /* 1 技术部 技术二部 1 技术部 技术三部 2 宣传部 no children 3 销售部 no children 4 网络部 网络二部 4 网络部 网络三部 5 技术二部 技术部1 6 网络二部 网络部1 7 网络三部 no children 8 技术三部 技术部2 9 网络部1 no children 10 技术部1 no children 11 技术部2 no children */
/*
标题:SQL SERVER 2005中树结构按照各顶级节点排序
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-10-24
地点:陕西西安
说明:
如下树级结构中需要按照各顶级节点排序
id pid name
---- ---- ----------
001 000 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇
011 000 江西省
012 011 南昌市
013 012 南昌县
014 012 新建县
015 012 进贤县
016 012 安义县
017 013 麻丘镇
018 011 九江市
需要的结果:
id name pid
---- ---------- ----
001 广东省 000
002 广州市 001
004 天河区 002
003 深圳市 001
005 罗湖区 003
006 福田区 003
007 宝安区 003
008 西乡镇 007
009 龙华镇 007
010 松岗镇 007
011 江西省 000
012 南昌市 011
013 南昌县 012
017 麻丘镇 013
014 新建县 012
015 进贤县 012
016 安义县 012
018 九江市 011
*/create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , '000' , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
insert into tb values('011' , '000' , N'江西省')
insert into tb values('012' , '011' , N'南昌市')
insert into tb values('013' , '012' , N'南昌县')
insert into tb values('014' , '012' , N'新建县')
insert into tb values('015' , '012' , N'进贤县')
insert into tb values('016' , '012' , N'安义县')
insert into tb values('017' , '013' , N'麻丘镇')
insert into tb values('018' , '011' , N'九江市')
go;with t as
(
select px = cast(id as varchar(500)) , * from tb t
where not exists(select 1 from tb where id = t.pid)
union all
select cast(b.px + ',' + a.id as varchar(500)) , a.*
from tb a , t b where a.pid = b.id
)
select id , name , pid from t order by pxdrop table tb
f not object_id('tempdb..#test') is null
begin
drop table #test
end
create table #test(DepartId int, DepartNamen nvarchar(36), ParentDepartId int, SortId int)insert into #test
select 1,'技术部',0,1 union all
select 2,'宣传部',0,2 union all
select 3,'销售部',0,3 union all
select 4,'网络部',0,4 union all
select 5,'技术二部',1,5 union all
select 6,'网络二部',4,6 union all
select 7,'网络三部',4,7 union all
select 8,'技术三部',1,8 union all
select 9,'网络部1',6,9 union all
select 10,'技术部1',5,10 union all
select 11,'技术部2',8,11
select a.DepartId as f_id,a.DepartNamen as f_name,isnull(b.DepartNamen,'no children') as children_name from #test a left join
#test b on a.DepartId=b.ParentDepartId
/*
1 技术部 技术二部
1 技术部 技术三部
2 宣传部 no children
3 销售部 no children
4 网络部 网络二部
4 网络部 网络三部
5 技术二部 技术部1
6 网络二部 网络部1
7 网络三部 no children
8 技术三部 技术部2
9 网络部1 no children
10 技术部1 no children
11 技术部2 no children
*/