2

I have an employee table which includes ManagerID as a foreign key. To get the currently logged ManagerId on Employees, I use a statement like -

SELECT MgrId FROM Employees WHERE EmpId=@EmpId 

@EmpId is currently Logged on Employees.

I need to get the currently logged on employees manager and the manager's manager in one statement. This involves using the output of the above statement as input of another select statement. I am unsure how to do this. Any help is much appreciated.

1
  • Look up for CTE : you'll do something like "With Manager (man) as (select MgrId from Employees WHERE EmpId=@EmpId) " then you can use the column man as a criteria in the following statement Commented May 13, 2015 at 11:35

1 Answer 1

2

Assuming your Employee ID field is named Id

SELECT e.Id [EmployeeId], e.MgrId [ManagerId], m.MgrId [ManagerManagerId]
FROM Employees e
LEFT JOIN Employees m ON e.MgrId = m.Id
WHERE e.EmpId=@EmpId
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.