SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Create table PrtChild ( Id integer primary key, PrtId integer null, Name varchar(20) ); insert into PrtChild values(1,null,'Root') insert into PrtChild values(2,1,'Child1') insert into PrtChild values(3,1,'Child2') insert into PrtChild values(4,2,'Grand Child 1'); with cteTree as ( Select Id,PrtId, Name as Path from PrtChild where PrtId is null union all Select child.Id, child.PrtID, child.name, cast(Cte.path + ' > ' + child.name as varchar(1000)) as path from CteTree Cte join PrtChild child on child.PrtId = Cte.Id ) select * from cteTree order by path

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear