修改 CTE 以引用另一个字段?
我有下面的 CTE,可以很好地递归员工表并构建传递给它的员工 ID 的扩展列表以及所有直接和间接报告。
我正在尝试让姐妹 CTE 使用用户名而不是员工 ID,但在进行更改后我没有收到任何数据...
这是我的工作代码与 employeeID
,我该如何使其与 NTID
(员工)和 managerNTID
(经理)一起使用
CREATE FUNCTION fnGetEmployeeHierarchy
(
@EmployeeId int = null
)
RETURNS TABLE
AS
RETURN
(
WITH yourcte AS
(
SELECT EmployeeId, ManagerID, NTID, FullName--, Name
FROM Employees
WHERE EmployeeId = isnull(@EmployeeId,EmployeeId)
UNION ALL
SELECT e.EmployeeId, e.ManagerID, e.NTID, e.FullName--, e.Name
FROM Employees e
JOIN yourcte y ON e.ManagerID = y.EmployeeId
)
SELECT EmployeeId, ManagerID, NTID, FullName--, Name
FROM yourcte
)
I have the CTE below working well in re-cursing an employee table and building an expanded list of the employee id passed to it as well as all direct, and indirect reports.
I'm trying to make a sister CTE to use user name instead of employee id, but I get no data back after I make the changes...
Here is my working code with employeeID
, how can I get this to work with NTID
(employee) and managerNTID
(manager)
CREATE FUNCTION fnGetEmployeeHierarchy
(
@EmployeeId int = null
)
RETURNS TABLE
AS
RETURN
(
WITH yourcte AS
(
SELECT EmployeeId, ManagerID, NTID, FullName--, Name
FROM Employees
WHERE EmployeeId = isnull(@EmployeeId,EmployeeId)
UNION ALL
SELECT e.EmployeeId, e.ManagerID, e.NTID, e.FullName--, e.Name
FROM Employees e
JOIN yourcte y ON e.ManagerID = y.EmployeeId
)
SELECT EmployeeId, ManagerID, NTID, FullName--, Name
FROM yourcte
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
managerNTID
引用经理的NTID
列 - 尝试以下操作:Assuming that
managerNTID
references the manager'sNTID
column - try this: