--查询部门及下属部门列表
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 --获取递归后的集合
结果: