日期:2014-05-18 浏览次数:20744 次
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
*/
------解决方案--------------------