I have a table with three columns NodeId, ParentNodeId, NodeName. for each node I would like to get a full path like "lvl1/lvl2/lvl3..." where lvl1,lvl2 and lvl3 are node names. I found a function which does that at this link http://www.sql-server-helper.com/functions/get-tree-path.aspx. but I would like to use CTE OR any other technique for efficiency. Please let me know if it is possible to achieve this in a better way. Thanks in advance.
Here's a CTE version.
declare @MyTable table (
NodeId int,
ParentNodeId int,
NodeName char(4)
)
insert into @MyTable
(NodeId, ParentNodeId, NodeName)
select 1, null, 'Lvl1' union all
select 2, 1, 'Lvl2' union all
select 3, 2, 'Lvl3'
declare @MyPath varchar(100)
;with cteLevels as (
select t.NodeId, t.ParentNodeId, t.NodeName, 1 as level
from @MyTable t
where t.ParentNodeId is null
union all
select t.NodeId, t.ParentNodeId, t.NodeName, c.level+1 as level
from @MyTable t
inner join cteLevels c
on t.ParentNodeId = c.NodeId
)
select @MyPath = case when @MyPath is null then NodeName else @MyPath + '/' + NodeName end
from cteLevels
order by level
select @MyPath
I solved it like this, much similar to Joe's solution.
with cte (NodeId,NodeName,hierarchyPath)as
(
select NodeId,NodeName, NodeName
from Node
where ParentNodeId is null
union all
select n.NodeId, n.NodeName, CONVERT(varchar(256), cte.hierarchyPath + '/' + n.NodeName)
from Node n
join cte on n.ParentNodeId = cte.NodeId
)
select *
from cte
order by NodeId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With