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
Hierarchyid 常用操作的更多相关文章
- 【三】用Markdown写blog的常用操作
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
- php模拟数据库常用操作效果
test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...
- Mac OS X常用操作入门指南
前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右) =鼠标左键 control+按下 ...
- mysql常用操作语句
mysql常用操作语句 1.mysql -u root -p 2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...
- nodejs配置及cmd常用操作
一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...
- Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理
摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■ 详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- Linux Shell数组常用操作详解
Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...
随机推荐
- C# 模拟提交 Form表单的数据
用 HttpWebRequest Post方法模拟提交Form表单数据时,需要设置 ContentType 为 "application/x-www-form-urlencoded" ...
- winform简单打印
首先新建一个winform 添加winform中自带的打印控件 winform中有默认的打印控件 1.按图片内容将控件拖拽到form中! 2.然后将pageSetupDialog1,printDial ...
- 获取MS SQL TABLE列名列表
在MS SQL Server中,想获取表的所有列名,可以使用下面SQL语句: SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[Columns] WHER ...
- SQL中 将同一个表中的A列更新到B列,B列更新到A列
有网友在SKYPE问及,如标题,SQL中 将同一个表中的A列更新到B列,B列更新到A列. 其实这个不是问题,直接写更新语句即可,可以参考下面动画演示: SQL source code: CREATE ...
- 在Android开发中调用Rest web服务(转)
首先从维基百科上拷贝一点Rest的基本概念给大家看看,然后我们再开始详解在Android中如何调用Rest服务.表象化状态转变(英文:Representational State Transfer,简 ...
- 炉石传说 C# 开发笔记(BS模式Demo)
原来这个项目,一直想做成CS模式的,BS模式对于炉石这样的游戏来说比较困难. 暴雪到现在也只出了 Windows 和 iPad版本的炉石,最大的问题还是在于如何在小屏幕下,实现最佳的客户体验. Win ...
- 去除utf8文件的bom标记
http://stackoverflow.com/questions/1068650/using-awk-to-remove-the-byte-order-mark http://thegreyblo ...
- 【书籍下载链接】_1_第一轮_C语言书籍
各位朋友,如果您觉得下载的电子书,看的还可以,请购买纸质版的图书,如果您觉得 您下载的书,不值得一看请在下载后直接删除. Windows汇编:http://dl.vmall.com/c0jk1v970 ...
- 泛函编程(29)-泛函实用结构:Trampoline-不再怕StackOverflow
泛函编程方式其中一个特点就是普遍地使用递归算法,而且有些地方还无法避免使用递归算法.比如说flatMap就是一种推进式的递归算法,没了它就无法使用for-comprehension,那么泛函编程也就无 ...
- 解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)
http://www.jb51.net/article/27616.htm 有次同事提出开发使用的mysql数据库连接很慢,因为我们的mysql开发数据库是单独一台机器部署的,所以认为可能是网络连 ...