Solved

Using hierarchyId in T-SQL

Posted on 2012-04-12
5
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 37841269
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
ID: 37841341
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
ID: 37841371
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
ID: 37844287
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
ID: 37845261
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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