So it's not the recursiveness that's the problem, it's the ordering of the output...? In that case you need to add some additional metadata (notice the TreePath):WITH Managers (Id, EmployeeName, EmployeesManagerId, ManagerLevel, TreePath) AS( SELECT ID, EmployeeName, EmployeesManagerId, 1 as ManagerLevel, '\' + CAST(ID AS varchar(max)) as TreePath FROM Employee WHERE EmployeesManagerId IS NULL UNION ALL SELECT e.ID, e.EmployeeName, e.EmployeesManagerId, ManagerLevel + 1 as ManagerLevel, m.TreePath + '\' + CAST(e.ID AS varchar(max)) FROM Employee e INNER JOIN Managers m ON e.EmployeesManagerId = m.ID)SELECT * FROM Managers ORDER BY TreePath
- LumbagoMy blog-> http://thefirstsql.com/2011/02/07/regular-expressions-advanced-string-matching-and-new-split-function-sql-server-2008-r2/