Contact Form

Name

Email *

Message *

Self join hierarchy in sql server like Menu GL Acount

No comments
;WITH HIERARCHY(Pagetitle, Parentid, ID, FullyQualifiedName, Level)
AS
(
 SELECT E.Pagetitle, E.Parentid, E.ID, CAST('.' + E.Pagetitle + '.' as NVarchar(MAX)), 0
FROM pages E
WHERE E.Parentid = 0
 UNION ALL
 SELECT E.Pagetitle, E.Parentid, E.ID, H.FullyQualifiedName + '.' + E.Pagetitle + '.', H.Level+1
FROM pages E
INNER JOIN HIERARCHY H on H.ID = E.Parentid
)

SELECT case when H.Level=0 then '->'+H.Pagetitle else H.Pagetitle end  as Pagetitle, H.Parentid, H.ID,  H.Level
 from HIERARCHY H
 order by   H.FullyQualifiedName

No comments :

Post a Comment