1
2--递归CTE
3
4Use tempdb
5Go
6
7Create Table Dept(
8 Id Int primary key,
9 Parent_Id int,
10 [name] nvarchar(50)
11)
12
13Insert Dept
14Select 0,0,N'<All>' Union All
15Select 1,0,N'财务部' Union All
16Select 2,0,N'行政部' Union All
17Select 3,0,N'业务部' Union All
18Select 4,3,N'软件开发' Union All
19Select 5,3,N'软件测试'
20Go
21
22--查询所有部门
23Declare @DeptName nvarchar(50)
24Set @DeptName = '业务部'
25
26;With
27CTE_Depts as
28(
29 --定位点成员
30 Select * From Dept
31 Where [name]=@DeptName
32 Union All
33 Select A.*
34 From Dept A,CTE_Depts B
35 Where A.Parent_Id = B.Id
36)
37
38Select * From CTE_Depts
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2009/03/22/1418756.html