|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-04-06 : 00:12:56
|
| [code]declare @temp table (NodeID INT, ParentNodeID INT, Name varchar(32), Level int)insert into @temp select 1 , null , 'Root' , 0insert into @temp select 2 , 1 , 'A' , 1insert into @temp select 3 , 1 , 'B' , 1insert into @temp select 4 , 2 , 'C' , 2insert into @temp select 5 , 2 , 'D' , 2insert into @temp select 6 , 3 , 'E' , 2insert into @temp select 7 , 3 , 'F' , 2insert into @temp select 8 , 4 , 'G' , 3 insert into @temp select 9 , 4 , 'H' , 3insert into @temp select 10 , 4 , 'I' , 3insert into @temp select 11 , 5 , 'J' , 3;with cte(nodeid,parentnodeid,name,path,level) as( select nodeid,parentnodeid,name,cast(nodeid as varchar(max)),0 from @temp where parentnodeid is null union all select t.nodeid,t.parentnodeid,t.name,c.path+'-'+cast(t.nodeid as varchar(max)),c.level+1 from @temp t inner join cte c on c.nodeid = t.parentnodeid)select nodeid,parentnodeid,name,level from cte order by path[/code] |
 |
|