Hierarchyid 常用操作
---------内置函数------------
select hierarchyid::GetRoot()--0x
select hierarchyid::Parse('/1/1/') --0x5AC0
select cast(0x5AC0 as hierarchyid)--0x5AC0
select cast('/1/' as hierarchyid)--0x5AC0
select cast(0x5AC0 as hierarchyid).ToString()--/1/1/
select cast(0x5AC0 as hierarchyid).GetLevel()--2
-----------IsDescendantOf------------ 判断@Node是否为@parent的子节点
declare @Node hierarchyid
declare @parent hierarchyid
set @Node='/1/2/3/4/'
set @parent='/1/2/'
select @Node.IsDescendantOf(@parent)--1
select @parent.IsDescendantOf(@Node)--0
--------GetAncestor ( n ) ----------返回指定层次的祖先.
declare @hy hierarchyid
declare @c int
set @hy='/1/1/2/1/'
set @c=@hy.GetLevel()
select @hy.GetAncestor(0).ToString()--/1/1/2/1/
select @hy.GetAncestor(1).ToString()--/1/1/2/
select @hy.GetAncestor(@c).ToString()--/
select @hy.GetAncestor(@c+1).ToString()--null
-----------GetDescendant-----------返回子集
//1.如果父级为NULL,则返回NULL。
---如果父级不为NULL----
--2.而child1 和child2 为NULL,则返回父级的子级。--
declare @hy hierarchyid
set @hy='/1/1/'
select @hy.GetDescendant(NULL,NULL).ToString()--/1/1/1/
--3. 返回值 在child1于child2之间 , child1>child2 且必须为@hy的子集--
select @hy.GetDescendant('/1/1/5/',null).ToString()--/1/1/6/
select @hy.GetDescendant(null,'/1/1/5/').ToString()--/1/1/4/
select @hy.GetDescendant('/1/1/2/','/1/1/5/').ToString()--/1/1/3/
select @hy.GetDescendant('/1/1/3/','/1/1/4/').ToString()--/1/1/3.1/
select @hy.GetDescendant(null,'/1/1/1/5/').ToString()--报异常
select @hy.GetDescendant('/1/1/5/','/1/1/3/').ToString()--报异常
---------◆GetReparentedValue :可以用来移动节点 --------------
注意:@parent是Node的祖先
declare @Node hierarchyid
DECLARE @NodeChild1 hierarchyid
declare @parent hierarchyid, @new hierarchyid
set @Node='/1/2/3/4/'
set @NodeChild1='/1/2/3/4/5/'
set @parent='/1/2/'
set @new='/5/6/7/'
SET @Node=@Node.GetReparentedValue(@parent, @new)
select @Node.ToString()--/5/6/7/3/4/
SELECT @NodeChild1.GetReparentedValue(hierarchyid::Parse('/1/2/3/4/'), @Node).ToString()
另一项常用操作是移动子树。下面的过程采用 @oldMgr 的子树作为参数,使其(包括 @oldMgr)成为 @newMgr 的子树。
CREATE PROCEDURE MoveOrg(@oldMgr nvarchar(256), @newMgr nvarchar(256) )
AS
BEGIN
DECLARE @nold hierarchyid, @nnew hierarchyid
SELECT @nold = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @oldMgr ;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
SELECT @nnew = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = @newMgr ;
SELECT @nnew = @nnew.GetDescendant(max(OrgNode), NULL)
FROM HumanResources.EmployeeDemo WHERE OrgNode.GetAncestor(1)=@nnew ;
UPDATE HumanResources.EmployeeDemo
SET OrgNode = OrgNode.GetReparentedValue(@nold, @nnew)
WHERE OrgNode.IsDescendantOf(@nold) = 1 ;
COMMIT TRANSACTION
END ;
GO
参考:
http://technet.microsoft.com/zh-cn/library/bb677212(v=sql.105).aspx
http://blog.csdn.net/tjvictor/article/details/4395681
http://msdn.microsoft.com/zh-cn/library/bb677290(v=sql.105).aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗