-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_Hierarchy_Data.sql
More file actions
182 lines (168 loc) · 5.51 KB
/
09_Hierarchy_Data.sql
File metadata and controls
182 lines (168 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**************************************************************
* SQL Server 2022 Hierarchy Data Tutorial
* Description: This script demonstrates working with hierarchical
* data using the hierarchyid data type in SQL Server.
* It covers table creation, data insertion, querying,
* updating, deleting, and advanced JSON and SQL Server 2022
* features for hierarchy representation.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating the Hierarchy Table
-------------------------------------------------
/*
1.1 Create a table using the hierarchyid data type.
The computed column OrgLevel is persisted for quick access.
*/
IF OBJECT_ID(N'dbo.Organization', N'U') IS NOT NULL
DROP TABLE dbo.Organization;
GO
CREATE TABLE dbo.Organization
(
OrgNode hierarchyid PRIMARY KEY,
OrgLevel AS OrgNode.GetLevel() PERSISTED,
OrgName NVARCHAR(100),
Manager NVARCHAR(100)
);
GO
-------------------------------------------------
-- Region: 2. Inserting Hierarchy Data
-------------------------------------------------
/*
2.1 Insert sample records into the Organization table.
Note: The hierarchyid::GetDescendant method is used to generate
hierarchical paths.
*/
INSERT INTO dbo.Organization (OrgNode, OrgName, Manager)
VALUES
(hierarchyid::GetRoot(), 'Company', 'CEO'),
(hierarchyid::GetRoot().GetDescendant(NULL, NULL), 'Division A', 'Manager A'),
(hierarchyid::GetRoot().GetDescendant(NULL, NULL).GetDescendant(NULL, NULL), 'Department A1', 'Manager A1'),
(hierarchyid::GetRoot().GetDescendant(NULL, NULL).GetDescendant(NULL, NULL).GetDescendant(NULL, NULL), 'Team A1-1', 'Manager A1-1'),
(hierarchyid::GetRoot().GetDescendant(NULL, NULL).GetDescendant(NULL, NULL).GetDescendant(NULL, hierarchyid::GetRoot().GetDescendant(NULL, NULL).GetDescendant(NULL, NULL).GetDescendant(NULL, NULL)), 'Team A1-2', 'Manager A1-2'),
(hierarchyid::GetRoot().GetDescendant(NULL, hierarchyid::GetRoot().GetDescendant(NULL, NULL)), 'Division B', 'Manager B'),
(hierarchyid::GetRoot().GetDescendant(NULL, hierarchyid::GetRoot().GetDescendant(NULL, NULL)).GetDescendant(NULL, NULL), 'Department B1', 'Manager B1');
GO
-------------------------------------------------
-- Region: 3. Querying the Hierarchy Data
-------------------------------------------------
/*
3.1 Retrieve the hierarchy data, ordering by the OrgNode.
*/
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager
FROM dbo.Organization
ORDER BY OrgNode;
GO
-------------------------------------------------
-- Region: 4. Indexing the Hierarchy Table
-------------------------------------------------
/*
4.1 Create a unique clustered index on the OrgNode column for better performance.
*/
CREATE UNIQUE CLUSTERED INDEX IX_Organization_OrgNode
ON dbo.Organization(OrgNode);
GO
-------------------------------------------------
-- Region: 5. Modifying and Deleting Hierarchy Data
-------------------------------------------------
/*
5.1 Update: Modify a record in the hierarchy.
*/
UPDATE dbo.Organization
SET OrgName = 'Division A Updated'
WHERE OrgNode = hierarchyid::GetRoot().GetDescendant(NULL, NULL);
GO
/*
5.2 Delete: Remove a record from the hierarchy.
*/
DELETE FROM dbo.Organization
WHERE OrgNode = hierarchyid::GetRoot().GetDescendant(NULL, hierarchyid::GetRoot().GetDescendant(NULL, NULL));
GO
-------------------------------------------------
-- Region: 6. Advanced Hierarchy Queries
-------------------------------------------------
/*
6.1 Get all descendants of a specific node.
*/
DECLARE @OrgNode hierarchyid = hierarchyid::GetRoot().GetDescendant(NULL, NULL);
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager
FROM dbo.Organization
WHERE OrgNode.IsDescendantOf(@OrgNode) = 1
ORDER BY OrgNode;
GO
/*
6.2 Get the parent of a specific node.
*/
DECLARE @ChildNode hierarchyid = hierarchyid::GetRoot().GetDescendant(NULL, NULL).GetDescendant(NULL, NULL);
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager
FROM dbo.Organization
WHERE OrgNode = @ChildNode.GetAncestor(1);
GO
/*
6.3 Get the root node.
*/
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager
FROM dbo.Organization
WHERE OrgNode = hierarchyid::GetRoot();
GO
-------------------------------------------------
-- Region: 7. Advanced JSON and SQL Server 2022 Hierarchy Queries
-------------------------------------------------
/*
7.1 Advanced query: Represent hierarchy data as a JSON snippet.
*/
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager,
JSON_QUERY(
(SELECT OrgNode.ToString() AS OrgNode, OrgLevel, OrgName, Manager
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
) AS OrgJSON
FROM dbo.Organization
ORDER BY OrgNode;
GO
/*
7.2 Advanced query using SQL Server 2022 JSON_OBJECT feature.
*/
SELECT
OrgNode.ToString() AS OrgNode,
OrgLevel,
OrgName,
Manager,
JSON_OBJECT(
'OrgNode' VALUE OrgNode.ToString(),
'OrgLevel' VALUE OrgLevel,
'OrgName' VALUE OrgName,
'Manager' VALUE Manager
) AS OrgJSON
FROM dbo.Organization
ORDER BY OrgNode;
GO
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------