Berikut ini contoh CTE untuk melakukannya:
declare @t table (id int, name varchar(max), parentid int)
insert into @t select 1, 'project' , 0
union all select 2, 'structure' , 1
union all select 3, 'path_1' , 2
union all select 4, 'path_2' , 2
union all select 5, 'path_3' , 2
union all select 6, 'path_4' , 3
union all select 7, 'path_5' , 4
union all select 8, 'path_6' , 5
; with CteAlias as (
select id, name, parentid
from @t t
where t.parentid = 0
union all
select t.id, parent.name + '\' + t.name, t.parentid
from @t t
inner join CteAlias parent on t.parentid = parent.id
)
select *
from CteAlias