Solved

Using hierarchyId in T-SQL

Posted on 2012-04-12
5
583 Views
Last Modified: 2012-08-14
Hello group,

I'm new to HierarchyId data type SQL Server 2008 and not sure why I have to pass a value as '/0/2/' to get descendent of a node in text. This is OK when there are limited number of records or having a very small table but when it gets to thousands or millions of records how should I use rather '/0/2/' ?



-- Get all the nodes under a specific node
select *, emp_id.GetLevel() as level, emp_id.ToString() as StringFormat
from employee
where emp_id.IsDescendantOf('/0/2/') = 1
order by emp_id;

Open in new window


Any help is appreciated.

Regards,
ak
0
Comment
Question by:akohan
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Nico Bontenbal
Comment Utility
I'm not sure I understand what your question is. Do you mean that with millions of records your  '/0/2/' string is getting very long. But at:
http://msdn.microsoft.com/en-us/library/bb677203.aspx
the sample is:
DECLARE @Manager hierarchyid
SELECT @Manager = OrgNode FROM HumanResources.EmployeeDemo
  WHERE LoginID = 'adventure-works\dylan0'

SELECT * FROM HumanResources.EmployeeDemo
WHERE OrgNode.IsDescendantOf(@Manager) = 1

Open in new window

So even if you have a million employees in the table and dylan is 10.000 levels deep this should still work without using the '/0/2/' string.
0
 

Author Comment

by:akohan
Comment Utility
Thanks but I guess my question was not clear ... what I meant was that in the above example /0/2/ is used as a constant but in a real world system you cannot know of every single record Hierarchy value which here is converted to string.
0
 
LVL 22

Assisted Solution

by:Nico Bontenbal
Nico Bontenbal earned 75 total points
Comment Utility
I understand. But in the example I gave you the string value of the hierarchyid is not used. It is retrieved based on the LoginID. So you use another field to get the record, and then retrieve the hierarchyid from this record. If "cannot know of every single record Hierarchy value" is your problem. Then the sample I gave you solves this problem, doesn't it? If not, please explain your problem in some more detail to me.
0
 
LVL 24

Accepted Solution

by:
DBAduck - Ben Miller earned 75 total points
Comment Utility
The way to use HierarchyId is illustrated in the first post.  You have to get the HierarchyId of the person or entity that you want to get the Descendants of and call it as illustrated.

The /0/2 is only an example and should never be used in real life.  You always want the value in the HierarchyId field to use for comparisons.
0
 

Author Comment

by:akohan
Comment Utility
Got that ... in fact today I went over more articles on msdn and understood it and your comments made me get the idea better.

Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now