Solved

Using hierarchyId in T-SQL

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

803 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