Say I have the following hierarchical representation in my database:
A
|_B_C
|_D
then I want to get the child nodes from A (or B). and vice versa, I want to get the parent from a given child node? <>
CREATE TABLE tbl (
Node HierarchyID PRIMARY KEY CLUSTERED,
NodeLevel AS Node.GetLevel(),
ID INT UNIQUE NOT NULL,
Name VARCHAR(50) NOT NULL
)
inserting the root:
INSERT INTO tbl (Node, ID, Name)
VALUES (HierarchyId::GetRoot(), 1, 'A')
child B
DECLARE @parent HierarchyId = HierarchyId::GetRoot()
INSERT INTO tbl (Node,ID,Name) VALUES (@parent.GetDescendant(NULL,NULL),2,'B')