国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Hierarchyid 常用操作

2024-07-21 02:49:06
字體:
來源:轉載
供稿:網友
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

?

?

?

????


上一篇:怎樣檢查fragmentation

下一篇:透視轉換

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达日县| 凤凰县| 昌图县| 霞浦县| 白沙| 贵州省| 开原市| 怀化市| 长丰县| 个旧市| 梁山县| 南川市| 玛沁县| 大方县| 乌审旗| 分宜县| 上杭县| 东港市| 鹤岗市| 禄丰县| 肥西县| 鄂托克旗| 祁门县| 高碑店市| 定南县| 广元市| 凉山| 延吉市| 稻城县| 伊吾县| 金塔县| 浦江县| 泗阳县| 南昌市| 南宫市| 杂多县| 普洱| 墨玉县| 阿合奇县| 通许县| 罗平县|