Hierarchyid (hierarchy) data type

Source: Internet
Author: User

Instance table structure

CREATE TABLE [dbo]. [EMP] ([ID] [int] IDENTITY (+), [Name] [varchar] (+), [ORG] [HierarchyID],) INSERT into tempdb. EMP (name,org) VALUES (' Wu xx ', '/') INSERT into tempdb. EMP (name,org) VALUES (' Xie xx ', '/1/') INSERT into tempdb. EMP (name,org) VALUES (' Zhao xx ', '/2/') INSERT into tempdb. EMP (name,org) VALUES (' Lou xx ', '/1/1/') INSERT into tempdb. EMP (name,org) VALUES (' Square xx ', '/1/2/') INSERT into tempdb. EMP (name,org) VALUES (' Zhan xx ', '/1/3/') INSERT into tempdb. EMP (name,org) VALUES (' Shen xx ', '/2/1/')

Instance base data

SELECT *,org.tostring (), Org.getlevel () from tempdb. Emp

  

HierarchyID Method Reference

ToString: Node path

--node. ToString () SELECT *,org.tostring () from tempdb: Emp

  

Getlevel: Node Depth

--node. Getlevel () SELECT *,org.getlevel () from tempdb. Empselect *,org.getlevel () from tempdb. Empwhere org.getlevel () = 1

  

GetAncestor: node n-level ancestor, that is, the child node of the specified level is reversed by the ancestor node.

--child. GetAncestor (N)--A Level two child node under the specified node select @CurrentNode = Org from tempdb. EMP WHERE Name = ' Wu xx ' SELECT * from tempdb. EMP WHERE org.getancestor (2) = @CurrentNode

Getdescendant: node nodes, greater than child1, and less than child2,child1/child2 can be null.

--parent. Getdescendant (CHILD1,CHILD2)-Added Wu Zi node, with Xie/playwriting level, but greater than Xie and less than Zhao. DECLARE @Child1 hierarchyiddeclare @Child2 hierarchyidselect @CurrentNode = Org from tempdb. EMP WHERE Name = ' Wu xx ' SELECT @Child1 = Org from tempdb. EMP WHERE Name = ' Xie xx ' SELECT @Child2 = Org from tempdb. EMP WHERE Name = ' Zhao xx ' INSERT into tempdb. EMP (name,org) VALUES (' in xx ', @CurrentNode. Getdescendant (@Child1, @Child2))

Isdescendantof: Whether it is a descendant node of a node

--child. Isdescendantof (parent)--the entire descendant node under the specified node, containing itself select @CurrentNode = Org from tempdb. EMP WHERE Name = ' Wu xx ' SELECT * from tempdb. EMP WHERE org.isdescendantof (@CurrentNode) = 1

Getreparentedvalue: The current node changes the new path, from Oldroot to Newroot

--node. Getreparentedvalue (Oldroot,newroot)--from point to Wu Zi node, update point to Jason node. DECLARE @OldRoot hierarchyiddeclare @NewRoot hierarchyidselect @CurrentNode = Org from tempdb. EMP WHERE Name = ' for xx ' SELECT @OldRoot = Org from tempdb. EMP WHERE Name = ' Wu xx ' SELECT @NewRoot = Org from tempdb. EMP WHERE Name = ' Zhao xx ' UPDATE tempdb: EMP SET [email protected] (@OldRoot, @NewRoot) WHERE Org = @CurrentNode

  

Getroot: Root node

--hierarchyid::getroot () SELECT *from tempdb. Empwhere Org = Hierarchyid::getroot ()

Parse: Converts from a path string to a HierarchyID value, which is re-encoded.

--hierarchyid::P arse () DECLARE @StringNode varchar (SET @StringNode = '/1/1/' SELECT *,org.tostring () from tempdb. EMP WHERE Org = HierarchyID::P arse (@StringNode)

Hierarchyid (hierarchy) data type

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.