Declare @YourTable Table ([NAME] varchar(50),[ID] varchar(50),[ManagerID] varchar(50))
Insert Into @YourTable Values
('John',1,3)
,('Sally',2,3)
,('Paul',3,4)
,('Jane',4,5)
,('Jennifer',5,8)
,('Boss',8,null)
Declare @Top int = 4 --<< NULL for Full Hier
Declare @Nest varchar(25) = '|-----' --<< Optional: Added for readability
;with cteP as (
Select Seq = cast(10000+Row_Number() over (Order by Name) as varchar(500))
,ID
,ManagerId
,Lvl=1
,Name
From @YourTable
Where IsNull(@Top,-1) = case when @Top is null then isnull(ManagerId ,-1) else ID end
Union All
Select Seq = cast(concat(p.Seq,'.',10000+Row_Number() over (Order by r.Name)) as varchar(500))
,r.ID
,r.ManagerId
,p.Lvl+1
,r.Name
From @YourTable r
Join cteP p on r.ManagerId = p.ID)
Select A.ID
,A.ManagerId
,A.Lvl
,Name = Replicate(@Nest,A.Lvl-1) + A.Name
From cteP A
Order By Seq
返回
ID ManagerId Lvl Name
4 5 1 Jane
3 4 2 |-----Paul
1 3 3 |-----|-----John
2 3 3 |-----|-----Sally