Do you mean this?--ID--------EMPNAME--------MANAGERID--__________________________________--1------------A-------------NULL --2------------B--------------1--3------------C--------------4--4------------D--------------2--5------------E--------------3--6------------F--------------5declare @sample table(id int, empname varchar(255), managerid int)insert @sampleselect 1,'A',NULL union allselect 2,'B',1 union allselect 3,'C',4 union allselect 4,'D',2 union allselect 5,'E',3 union allselect 6,'F',5--select * from @sample;WITH cte (managerid,id,empname)AS(-- Anchor member definition SELECT e.ManagerID, e.ID, e.empname FROM @sample as e WHERE ManagerID IS NULL UNION ALL-- Recursive member definition SELECT e.ManagerID, e.ID, e.empname FROM @sample as e INNER JOIN cte AS d ON e.ManagerID = d.ID)select id,empname,managerid from cte
I have taken an example from BOL and adapted it to your data.
No, you're never too old to Yak'n'Roll if you're too young to die.