sql server 递归查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 递归查询相关的知识,希望对你有一定的参考价值。
Q201410000140 UENT3512864630201403100072
Q201410000141 UENT3512864630201405100097
Q201410000141 UENT3512864630201403100072
Q201410000142 UENT3512864630201405100097
如上数据,同一张表,二列, 要求存储过程中 写一个递归查询 如:
通过 Q201410000140 编号能查询出 UENT3512864630201403100072
之后再通过:UENT3512864630201403100072 查询出 Q201410000141
之后再通过:Q201410000141 查询出 UENT3512864630201405100097
之后再通过:UENT3512864630201405100097 查询出 Q201410000142
重复的不需再查,最终将所有关联的都查询出来当做一组数据。
答出再给加分,要只能在过程中写。
请写出SQL 再加分
insert into tb values('001' , null , '广东省');
insert into tb values('002' , '001' , '广州市');
insert into tb values('003' , '001' , '深圳市') ;
insert into tb values('004' , '002' , '天河区') ;
insert into tb values('005' , '003' , '罗湖区');
insert into tb values('006' , '003' , '福田区') ;
insert into tb values('007' , '003' , '宝安区') ;
insert into tb values('008' , '007' , '西乡镇') ;
insert into tb values('009' , '007' , '龙华镇');
insert into tb values('010' , '007' , '松岗镇')WITH EmpsCTE AS
(
SELECT EmployeeID, ReportsTo, FirstName, LastName
FROM dbo.Employees
WHERE EmployeeID = 5
UNION ALL
SELECT EMP.EmployeeID, EMP.ReportsTo, EMP.FirstName, EMP.LastName
FROM EmpsCTE AS MGR
JOIN dbo.Employees AS EMP
ON EMP.ReportsTo = MGR.EmployeeID
)
SELECT * FROM EmpsCTE;
这就是一个使用CTE的一个查询,你看一下吧 参考技术B WITH EmpsCTE AS
(
SELECT EmployeeID, ReportsTo, FirstName, LastName
FROM dbo.Employees
WHERE EmployeeID = 5
UNION ALL
SELECT EMP.EmployeeID, EMP.ReportsTo, EMP.FirstName, EMP.LastName
FROM EmpsCTE AS MGR
JOIN dbo.Employees AS EMP
ON EMP.ReportsTo = MGR.EmployeeID
)
SELECT * FROM EmpsCTE;
这就是一个使用CTE的一个查询,你看一下吧
Sql ServerSQL SERVER 递归查询
SQL SERVER 2005之前的版本只能用函数方法实现,SQL SERVER 2005之后新增了CTE功能,可以利用CTE实现递归查询;
CTE:公用表达式Common Table Expression 是SQL SERVER 2005版本之后引入的一个特性;
#填充测试数据
1、sql
1 Create table GroupInfo([Id] int,[GroupName] nvarchar(50),[ParentGroupId] int) 2 3 Insert GroupInfo 4 5 select 0,\'某某大学\',null union all 6 7 select 1,\'外语学院\',0 union all 8 select 2,\'英语专业\',1 union all 9 select 3,\'日语专业\',1 union all 10 select 4,\'英语专业一班\',2 union all 11 select 5,\'英语专业二班\',2 union all 12 select 6,\'日语专业一班\',3 union all 13 select 7,\'日语专业二班\',3 union all 14 15 select 8, \'法学院\',0 union all 16 select 9, \'刑法学专业\',8 union all 17 select 10,\'经济法学专业\',8 union all 18 select 11,\'刑法学专业一班\',9 union all 19 select 12,\'刑法学专业二班\',9 union all 20 select 13,\'经济法学专业一班\',10 union all 21 select 14,\'经济法学专业二班\',10
2、效果图
#递归实现Demo
1、根据指定的节点向上获取所有父节点,向下获取所有子节点
1 --根据指定的节点向下获取所有子节点 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=1 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.Id=G.ParentGroupId 10 ) 11 select * from CTE order by Id
1 --根据指定的节点向上获取所有父节点 2 with 3 CTE 4 as 5 ( 6 select * from GroupInfo where Id=14 7 union all 8 select G.* from CTE inner join GroupInfo as G 9 on CTE.ParentGroupId=G.Id 10 ) 11 select * from CTE order by Id
2、构造递归路径
1 --构造递归路径 2 with 3 CTE 4 as 5 ( 6 select Id,GroupName,ParentGroupId,GroupPath=CAST( GroupName as nvarchar(max)) from GroupInfo where Id=1 7 union all 8 select G.*,CAST(CTE.GroupPath+\'//\'+G.GroupName as nvarchar(max)) as GroupPath from CTE 9 inner join GroupInfo as G 10 on CTE.Id=G.ParentGroupId 11 ) 12 select * from CTE
3、分组递归,将同一条分支上节点放到一起
1 --通过id字段的字符串的拼接,形成sort字段,再通过sort排序,来实现同一分支上的节点放到一起 2 WITH 3 CTE 4 AS 5 ( 6 SELECT * ,CAST(RIGHT(\'000\' + CAST([Id] AS VARCHAR), 3) AS VARCHAR(MAX)) AS sort FROM GroupInfo 7 WHERE ParentGroupId = 0 8 UNION ALL 9 SELECT GroupInfo.* ,CAST(sort + RIGHT(\'000\' + CAST(GroupInfo.[Id] AS VARCHAR),3) AS VARCHAR(MAX)) AS sort 10 FROM CTE 11 INNER JOIN GroupInfo ON CTE.Id = GroupInfo.ParentGroupId 12 ) 13 SELECT * FROM CTE ORDER BY sort
4、递归层级查询(查询出节点所属的层级)
1 --查询节点层级 2 WITH CTE AS ( 3 SELECT *,1 AS [Level] FROM GroupInfo WHERE ParentGroupId=0 4 UNION ALL 5 SELECT G.*,CTE.Level+1 FROM GroupInfo as G 6 JOIN CTE ON CTE.Id =G.ParentGroupId 7 ) 8 SELECT * FROM CTE
以上是关于sql server 递归查询的主要内容,如果未能解决你的问题,请参考以下文章