Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using hierarchyId in T-SQL

Posted on 2012-04-12
5
Medium Priority
?
641 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 300 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 25

Accepted Solution

by:
DBAduck - Ben Miller earned 300 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

927 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