1. 程式人生 > >HierarchyId通過父節點建立一個新的子節點

HierarchyId通過父節點建立一個新的子節點

--HierarchyId通過父節點建立一個新的子節點
CREATE TABLE #temp(
node HierarchyID
);

insert into #temp
select '/' union all
select '/1/' union all
select '/2/' union all
select '/1/1/' union all
select '/1/2/' union all
select '/1/1/1/' union all
select '/1/1/1/1/'

declare @HyId hierarchyid=HierarchyID::Parse('/1/2/');--父級節點
declare @NewHyId
hierarchyid; select @NewHyId=@HyId.GetDescendant(MAX(node),null) from #temp Where node.GetAncestor(1)=@HyId--建立一個新的節點 insert into #temp(node) values(@NewHyId) select *,node.ToString() from #temp