SqlServer 递归查询

--查询部门及下属部门列表

WITH TEMP   --递归
     AS (SELECT Id,
                Code,
                Name,
                ParentId
         FROM   [dbo].[AspSysDepartments]
         WHERE  Id = 38   --查询当前部门
         UNION ALL
         SELECT B.Id,  --查询子部门
                B.Code,
                B.Name,
                B.ParentId
         FROM   TEMP A
                INNER JOIN [dbo].[AspSysDepartments] B
                        ON B.ParentId = A.Id)
SELECT Id,
       Code,
       Name,
       ParentId
FROM   TEMP   --获取递归后的集合

结果:

 

posted @ 2019-01-23 11:02  _York  阅读(12961)  评论(2编辑  收藏  举报