with cte(parent_id,id) as ( select parent_id,Id from table where parent_id is null union all select parent_id,ID from table as a inner join cte as b on b.id=a.parent_id ) select *from cte
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)