Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
evvo1961
Starting Member
4 Posts |
Posted - 2013-10-21 : 02:58:23
|
I am wanting to run a SQL statement whereby i return the ID of any employee's Director.The database for employees has a reportsto field which enables me to see the hierarchy of managers above any employee.There is also a IsDirector flag that indicates a director.So essentially i want to run sql that would return the first instance of a director in the hierarchy above any employee.eg if A reports to B and B reports to C (who is a director) then it returns C.I basically want the script to run until a director is found.how would i do this? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 06:30:44
|
use a recursive CTEDECLARE @EmpName varchar(100)SET @EMpName = <pass a value here>;With ManagerInfoAS(SELECT ID,Name AS EmpName, ReportsTo, 1 AS level,IsDirectorFROM TableWHERE Name = @YourEmpNameUNION ALLSELECT t.ID,t.Name,t.ReportsTo,m.Leval + 1 ,t.IsDirector FROM ManagerInfo mINNER JOIN Table tON t.ID = m.ReportsTo)SELECT TOP 1 NameFROM ManagerInfoWHERE Name <> @EmpNameAND IsDirector = 1ORDER BY LevelOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
evvo1961
Starting Member
4 Posts |
Posted - 2013-10-21 : 09:06:37
|
thanksi had read up on this but cannot seem to get it working...this is the code im trying to get working but it returns no recordsDECLARE @UserID INT = NULLSET @UserID = 595;With ManagerInfoAS(SELECT u.UserID, u.Username, u.ReportsTo, 1 AS level, p.IsDirectorFROM Users uINNER JOIN Profiles pON u.UserID = p.UserIDWHERE u.UserID = @UserIDUNION ALLSELECT u.UserID, u.username, u.ReportsTo, m.Level + 1 , m.IsDirector FROM ManagerInfo mINNER JOIN Users uON u.UserID = m.ReportsTo)SELECT TOP 1 UserIDFROM ManagerInfoWHERE UserID <> @UserIDAND IsDirector = 1ORDER BY LevelOPTION (MAXRECURSION 0) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 09:13:18
|
its because you're not retrieving isdirector value properly in recursive partdo this small tweak and seeDECLARE @UserID INT = NULLSET @UserID = 595;With ManagerInfoAS(SELECT u.UserID, u.Username, u.ReportsTo, 1 AS level, p.IsDirectorFROM Users uINNER JOIN Profiles pON u.UserID = p.UserIDWHERE u.UserID = @UserIDUNION ALLSELECT u.UserID, u.username, u.ReportsTo, m.Level + 1 , p.IsDirector FROM ManagerInfo mINNER JOIN Users uON u.UserID = m.ReportsToINNER JOIN Profiles pON u.UserID = p.UserID)SELECT TOP 1 UserIDFROM ManagerInfoWHERE UserID <> @UserIDAND IsDirector = 1ORDER BY LevelOPTION (MAXRECURSION 0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
evvo1961
Starting Member
4 Posts |
Posted - 2013-10-21 : 09:25:04
|
ah yes thanks that does the trickmany thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 09:34:14
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|