这个SQL语句要怎么写??

simon__sun 2010-08-07 09:42:09
有一张部门表:
部门编号 部门名称 上级部门编号
1 tes1 0
2 tes2 0
3 tes3 2
4 tes4 1
5 tes5 3
6 tes6 3
7 tes7 4

现在要通过一条sql语句列出所有该部门及子部门,子部门层级不限。 如部门编是2 要怎么写得到下面的内容,

2 tes2 0
3 tes3 2
5 tes5 3
6 tes6 3
...全文
115 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspNet123Xu 2010-08-08
  • 打赏
  • 举报
回复

DECLARE @depno int;
SET @depno=3;

with cte(部门编号,部门名称,上级部门编号,lvl)
AS
(
SELECT 部门编号,部门名称,上级部门编号,0 FROM # WHERE 部门编号=@depno
UNION ALL
SELECT tb.部门编号,tb.部门名称,tb.上级部门编号,lvl+1 FROM # tb JOIN cte ON tb.上级部门编号=cte.部门编号
)
select 部门编号,部门名称,上级部门编号,lvl '部门阶层' from cte


3 tes3 2 0
5 tes5 3 1
6 tes6 3 1
leelin2010 2010-08-08
  • 打赏
  • 举报
回复
with cte as
(
select * from tableName where 部门编号=2
union all
select * from tableName a
join cte t on a.上级部门编号=t.部门编号
)
select * from cte
fly_Apple10 2010-08-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/fly_Apple10/archive/2010/08/08/5796667.aspx



类似的
今天上午写的
hao1hao2hao3 2010-08-07
  • 打赏
  • 举报
回复


好像写反了,

with cte as
(
select * from tab where 部门编号 = 2
union all
select tab.* from tab join cte on tab.上级部门编号= cte.部门编号
)
select * from cte
hao1hao2hao3 2010-08-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hao1hao2hao3 的回复:]
SQL code

with cte as
(
select * from tab where 上级部门编号 = 0
union all
select * from tab join cte on tab.上级部门编号= cte.部门编号
)
select * from cte
[/Quote]

不好意思,应该是


with cte as
(
select * from tab where 上级部门编号 = 0
union all
select tab.* from tab join cte on tab.上级部门编号= cte.部门编号
)
select * from cte
duanzhi1984 2010-08-07
  • 打赏
  • 举报
回复

create table #t(部门编号 int,部门名称 varchar(10), 上级部门编号 int)

insert into #t

select 1,'tes1',0 union all
select 2,'tes2',0 union all
select 3,'tes3',2 union all
select 4,'tes4',1union all
select 5,'tes5',3union all
select 6,'tes6',3union all
select 7,'tes7', 4

with c (部门编号,部门名称,上级部门编号) as
(
select 部门编号,部门名称,上级部门编号 from #t where 部门编号=2
union all
select a.部门编号,a.部门名称,a.上级部门编号 from #t a join c on a.上级部门编号=c.部门编号
)
select *from c

2 tes2 0
3 tes3 2
5 tes5 3
6 tes6 3


rucypli 2010-08-07
  • 打赏
  • 举报
回复
with cte as(
select a from test A where b=2
union all
select B.a from cte A ,test B where B.b=A.a
)
select * from cte
SQLCenter 2010-08-07
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(部门编号 int, 部门名称 varchar(8), 上级部门编号 int)
insert into #
select 1, 'tes1', 0 union all
select 2, 'tes2', 0 union all
select 3, 'tes3', 2 union all
select 4, 'tes4', 1 union all
select 5, 'tes5', 3 union all
select 6, 'tes6', 3 union all
select 7, 'tes7', 4

declare @dept int
set @dept = 2;

with t as
(
select * from # where 部门编号 = @dept
union all
select #.* from # join t on #.上级部门编号 = t.部门编号
)
select * from t

/*
部门编号 部门名称 上级部门编号
----------- -------- -----------
2 tes2 0
3 tes3 2
5 tes5 3
6 tes6 3
*/
hao1hao2hao3 2010-08-07
  • 打赏
  • 举报
回复

with cte as
(
select * from tab where 上级部门编号 = 0
union all
select * from tab join cte on tab.上级部门编号= cte.部门编号
)
select * from cte
duanzhi1984 2010-08-07
  • 打赏
  • 举报
回复
SQL2005 with cte 递归

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧