Common hierarchyid operations

Source: Internet
Author: User

?

?

--------- Built-in function ------------

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 ------------ determine whether @ node is a subnode of @ 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) ---------- return the ancestor of the specified level.

Declare @ hy hierarchyid

Declare @ C int

Set @ hy = '/1/1/2/1 /'

Set @ [email protected] ()

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 ----------- returns a subset.

// 1. If the parent level is null, null is returned.

?

--- If the parent level is not null ----

-- 2. If Child1 and child2 are null, the parent child is returned. --

Declare @ hy hierarchyid

Set @ hy = '/1/1 /'

Select @ Hy. getdescendant (null, null). tostring () --/1/1/1/

?

-- 3. The returned value is between Child1 and child2. Child1> child2 must be a subset of @ 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 () -- an exception is reported.

Select @ Hy. getdescendant ('/1/1/5/', '/1/1/3/'). tostring () -- an exception is reported.

?

--------- ◆ Getreparentedvalue: can be used to move nodes --------------

Note: @ parent is the node's ancestor

?

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 @ [email protected] (@ parent, @ new)

Select @ node. tostring () --/5/6/7/3/4/

Select @ nodechild1.getreparentedvalue (hierarchyid: parse ('/1/2/3/4/'), @ node). tostring ()

?

?

Move subtree

Another common operation is to move the subtree. Which of the following processes is used?@ Oldmgr? As a parameter to make it (including?@ Oldmgr) Become?@ 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) [email protected];

?

Update HumanResources. employeedemo

Set orgnode = orgnode. getreparentedvalue (@ nold, @ nNew)

Where orgnode. isdescendantof (@ nold) = 1;

?

Commit transaction

End;

Go

?

?

?

Refer:

?

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

?

?

?

????

Common hierarchyid operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.