Based on an existing table I used CTE recursive query to come up with following data. But failing to apply it a level further.
Data is as below
id nam
try this:
DECLARE @YourTable table (id int, nameof varchar(25), parentid int)
INSERT @YourTable VALUES (1,'project',0)
INSERT @YourTable VALUES (2,'structure',1)
INSERT @YourTable VALUES (3,'path_1',2)
INSERT @YourTable VALUES (4,'path_2',2)
INSERT @YourTable VALUES (5,'path_3',2)
INSERT @YourTable VALUES (6,'path_4',3)
INSERT @YourTable VALUES (7,'path_5',4)
INSERT @YourTable VALUES (8,'path_6',5)
;WITH Rec AS
(
SELECT
CONVERT(varchar(max),nameof) as nameof,id
FROM @YourTable
WHERE parentid=0
UNION ALL
SELECT
CONVERT(varchar(max),r.nameof+'\'+y.nameof), y.id
FROM @yourTable y
INNER jOIN Rec r ON y.parentid=r.id
)
select * from rec
output:
nameof
-----------------------------------------------
project
project\structure
project\structure\path_1
project\structure\path_2
project\structure\path_3
project\structure\path_3\path_6
project\structure\path_2\path_5
project\structure\path_1\path_4
(8 row(s) affected)