1

I am creating multi level hierarchy in SQL Server. How can I create that?

This my table data-

   ID DeptID ParentID FolderName
   1  1      0        2
   2  1      1        2.1
   3  1      1        2.2
   4  1      1        2.3
   5  1      2        2.1.1
   6  1      2        2.1.2
   7  1      2        2.1.3
   8  1      2        2.1.4
   9  1      5        2.1.1.1
   10 1      5        2.1.1.2
   11 1      5        2.1.1.3
   12 2      0        1
   13 3      0        3

I want following result.

   ID DeptID ParentID FolderName
   1  1      0        2
   2  1      1        2.1
   5  1      2        2.1.1
   9  1      5        2.1.1.1
   10 1      5        2.1.1.2
   11 1      5        2.1.1.3
   6  1      2        2.1.2
   7  1      2        2.1.3
   8  1      2        2.1.4
   3  1      1        2.2
   4  1      1        2.3
   12 2      0        1
   13 3      0        3

1 Answer 1

3

If you already have the FolderName column, some simple replacement and conversions turns it into a hierarchyid which already understands how to sort hierarchically:

declare @t table (ID int,DeptID int, ParentID int, FolderName varchar(900))
insert into @t(ID, DeptID, ParentID, FolderName) values
(1 ,1,0,'2'),
(2 ,1,1,'2.1'),
(3 ,1,1,'2.2'),
(4 ,1,1,'2.3'),
(5 ,1,2,'2.1.1'),
(6 ,1,2,'2.1.2'),
(7 ,1,2,'2.1.3'),
(8 ,1,2,'2.1.4'),
(9 ,1,5,'2.1.1.1'),
(10,1,5,'2.1.1.2'),
(11,1,5,'2.1.1.3'),
(12,2,0,'1'),
(13,3,0,'3')

;With Abc as (
    select
        *,
        CAST('/' + REPLACE(FolderName,'.','/') + '/' as hierarchyid) as FolderNameRightType
    from
        @t
)
select
    *
from
    Abc
order by FolderNameRightType

Result:

ID          DeptID      ParentID    FolderName           FolderNameRightType
----------- ----------- ----------- -------------------- -----------------------
12          2           0           1                    0x58
1           1           0           2                    0x68
2           1           1           2.1                  0x6AC0
5           1           2           2.1.1                0x6AD6
9           1           5           2.1.1.1              0x6AD6B0
10          1           5           2.1.1.2              0x6AD6D0
11          1           5           2.1.1.3              0x6AD6F0
6           1           2           2.1.2                0x6ADA
7           1           2           2.1.3                0x6ADE
8           1           2           2.1.4                0x6AE1
3           1           1           2.2                  0x6B40
4           1           1           2.3                  0x6BC0
13          3           0           3                    0x78
Sign up to request clarification or add additional context in comments.

1 Comment

mentioned folder name values are dummy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.