USE Northwind
go
select employeeid ,reportsto from employees;
--察看2的子节点,右边是左边的父节点
with myT as(
select employeeid ,reportsto from employees
),
myT2 as(
select * from myT where reportsto=2
union all
select myT.* from myT2 inner join myT
on myT2.employeeid=myT.reportsto
)
select * from myT2
--解除递归100次的限制
option(maxrecursion 0)

本文介绍了一种使用SQL递归查询的方法来查找组织结构中员工及其下属的信息。通过创建临时表并使用联合操作,实现了从数据库中获取指定员工的所有直接和间接下属的数据。
110

被折叠的 条评论
为什么被折叠?



