Solved

Using hierarchyId in T-SQL

Posted on 2012-04-12
5
601 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 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