--查询所有的部门子ID
WITH dept
AS (
SELECT *
FROM dbo.[T_DepartmentInfo]
WHERE [ParentDepId] = @id--ParentDepId查询不包括自己,改为主键ID查询就包括自己和下面所有的部门子ID
UNION ALL
SELECT d.*
FROM dbo.[T_DepartmentInfo] d
INNER JOIN dept ON d.[ParentDepId] = dept.[DepId]
)
SELECT DepId,DepName,ParentDepId
FROM dept