假设我的数据库中有以下分层表示:
A
|_B_C
|_D
然后我想从A(或B)获取子节点。反之亦然,我想从给定的子节点获取父节点?<>
CREATE TABLE tbl (
Node HierarchyID PRIMARY KEY CLUSTERED,
NodeLevel AS Node.GetLevel(),
ID INT UNIQUE NOT NULL,
Name VARCHAR(50) NOT NULL
)
插入根:
INSERT INTO tbl (Node, ID, Name)
VALUES (HierarchyId::GetRoot(), 1, 'A')
孩子乙
DECLARE @parent HierarchyId = HierarchyId::GetRoot()
INSERT INTO tbl (Node,ID,Name) VALUES (@parent.GetDescendant(NULL,NULL),2,'B')