如何在SQL Server中创建多级层次结构

时间:2018-12-27 12:33:33

标签: sql-server sql-server-2012

我正在SQL Server中创建多级层次结构。我该如何创建?

这是我的表格数据-

   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

我要跟踪结果。

   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 个答案:

答案 0 :(得分:3)

如果您已经有了FolderName列,则可以通过一些简单的替换和转换将其转换为hierarchyid,该列已经了解如何进行分层排序:

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

结果:

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