I`ve got a table with records linking to each other
name id manager_id
____________________
adam 1 Null
nick 2 1
sten 3 1
iren 4 3
john 5 2
steve 6 3
I want a Stored Procedure which will return a banch of managers for current worker. For example if i ask for Steve`s managers query result should look like that
steve 6 3
sten 3 1
adam 1 Null
How can it be done?
(it is not necessary to avoid loops for now)
From SQL 2005 onwards, you can use a recursive CTE, like below:
Or, as of SQL 2008, you could use a different approach using the new HIERARCHYID datatype, which is a new way to represent hierarchical data.