Skip to content Skip to sidebar Skip to footer

Get Hierarchical Structure Using Sql Server

I have a self-referencing table with a primary key, id and a foreign key parent_id. +------------+--------------+------+-----+---------+----------------+ | Field | Type

Solution 1:

You can use Common Table Expressions.

WITH LeveledSiteMap(Id, Name, Level)
AS
(
    SELECT Id, Name, 1AS Level
    FROM MySiteMap
    WHERE Parent_Id IS NULL
    UNION ALL
    SELECT m.Id, m.Name, l.Level + 1FROM MySiteMap AS m
        INNER JOIN LeveledSiteMap AS l
        ON m.Parent_Id = l.Id
)
SELECT *
FROM LeveledSiteMap

Solution 2:

Use this:

;WITH CTE(Id, Name, parent_id, [Level], ord) AS (
    SELECT 
        MySiteMap.Id, 
        CONVERT(nvarchar(255), MySiteMap.Name) AS Name, 
        MySiteMap.parent_id, 
        1,
        CONVERT(nvarchar(255), MySiteMap.Id) AS ord
    FROM MySiteMap
    WHERE MySiteMap.parent_id ISNULLUNIONALLSELECT 
        MySiteMap.Id, 
        CONVERT(nvarchar(255), REPLICATE(' ', [Level]) +'|'+ REPLICATE(' ', [Level]) + MySiteMap.Name) AS Name, 
        MySiteMap.parent_id, 
        CTE.[Level] +1,
        CONVERT(nvarchar(255),CTE.ord +CONVERT(nvarchar(255), MySiteMap.Id)) AS ord
    FROM MySiteMap
        JOIN CTE ON MySiteMap.parent_id =CTE.Id
    WHERE MySiteMap.parent_id ISNOTNULL
)
SELECT Name
FROM CTE
ORDERBY ord

For this:

A
 | B
  |  B1
  |  B2
 | C
  |  C1
  |  C2
 | D
  |  D1
  |  D2

Solution 3:

I started with a query, (but when I check it now it is similar to Mark.)

I will add it anyway, while I created also a sqlfiddle with mine and Mark query.

WITH tList (id,name,parent_id,nameLevel)
AS
(
    SELECT t.id, t.name, t.parent_id, 1AS nameLevel
    FROM t as t
    WHERE t.parent_id IS NULL
    UNION ALL
    SELECT tnext.id, tnext.name, tnext.parent_id, tList.nameLevel + 1FROM t AS tnext
    INNER JOIN tList AS tlist
        ON tnext.parent_id = tlist.id
)
SELECT id,name,isnull(parent_id,0) 'parent_id',nameLevel FROM tList order by nameLevel;

A good blog: SQL Query – How to get data in Hierarchical Structure?

Solution 4:

i know changing the structure of a table is always a critical operation but since sql server 2008 introduced the HierarchyId Datatype i really like workig with it. Maybe have a look at:

http://www.codeproject.com/Articles/37171/HierarchyID-Data-Type-in-SQL-Serverhttp://www.codeproject.com/Tips/740553/Hierarchy-ID-in-SQL-Server

I am sure you will understand quickly how to use this datatype and his functions. The SQL Code using this datatype is more structured and has better performance than CTE's.

Post a Comment for "Get Hierarchical Structure Using Sql Server"