1 ;With cte(id,pid,TName)As 2 ( 3 Select id,pid,TName 4 From Test where pid = 1 5 Union All 6 Select B.id,B.pid,B.TName 7 From cte Inner Join Test B On cte.id = B.pid 8 ) 9 select * from cte order by id