Building a TreeView ResultSet with SQL Server 2005
page 1 of 5
Published: 02 Oct 2006
This article is an upgrade of my previous article on Building Tree View Resultset. It explains how we can create the Tree View Resultset with SQL 2005 with just one SQL Statement.
by Ameet Phadnis
Average Rating: 
Views (Total / Last 10 Days): 29265/ 48


In a corporate environment we usually face coding tasks to display results in Treeview.  The tasks may include displaying corporate structure or in programs displaying the menu structure.  Around 5 to 6 years ago when I was tasked with a similar kind of assignment, my manager wanted me to get the results through the SQL Stored Procedure and then the web front end would do the rest.  I came up with a way to create the tree hierarchy in SQL Server 2000.  It was very complex and hard to understand.  We had no idea how much SQL Server 2005 would simplify our lives.  I have explained the procedure in my previous article.  You can view the article by clicking here.

Before I give you the SQL Script, I would like to explain some of the new features introduced in SQL Server 2005.

View Entire Article

User Comments

Title: Hierarchical structure   
Name: Venkat
Date: 2008-12-22 8:08:36 AM
Before getting this site
I was much confused to solve query in Hierarchical.
its very helpful to all...
Title: Works like a dream!   
Name: Per
Date: 2008-12-20 1:51:09 AM
Just make sure you have an appropriate WHERE clause for your first 'anchor query'. In the example above I gather it should be:
'... 0 as HLevel FROM tblDepartments WHERE ParentDepartmentID is null'
instead of:
'... 0 as HLevel FROM tblDepartments'
Title: Thanks   
Name: Raheel
Date: 2008-05-11 11:50:21 PM
Nieece ... For a moment I though I should leave IT ...Thanks Ameet ..and Thanks Google
Title: SQL/XML format string   
Name: Jayeff_Land
Date: 2008-04-08 1:57:53 PM
I have found my own answer on the web. If anybody is interested :
Title: SQL/XML format string   
Name: Jayeff_Land
Date: 2008-04-08 1:55:04 PM
I have been seaching for a really good example for that! It was really simple and helpful.
Is it possible to transfer the result in a XML format string?
Title: Very Helpful   
Name: Felipe Bossolani
Date: 2007-03-20 11:08:18 AM
Sounds Perfect and Works PERFECT!
Title: Cannot get the subcategories under the main ones   
Name: Jovanky De Los Santos
Date: 2007-03-16 10:35:03 AM
My Table Structure

Category_ID Number
Parent_ID Number <----Category_ID reports to this colum
Category_Name Varchar....

MY QUERY <---I replaced the query above with my data
WITH Hierarchy(Category_ID, Category_Name, Parent_ID, HLevel)
SELECT Category_ID, Category_Name, Parent_ID, 0 as HLevel FROM Dir_Categories
SELECT SubCategory.Category_ID
, SubCategory.Category_Name,
HLevel + 1
FROM Dir_Categories SubCategory
INNER JOIN Hierarchy ParentCategory
ON SubCategory.Parent_ID = ParentCategory.Category_ID )
SELECT Category_ID,
Category_Name = Replicate('__', HLevel) + Category_Name,
FROM Hierarchy

My OUTPUT============

All the categories under reporting to Parent_ID 0 or continuous, then the ones reporting to 1 and so fourth. Subcategories are not showing within their main categories. I AM GOING NUTS WITH THIS.

Can you help me please?
Title: What about employees   
Name: Rodney
Date: 2007-01-25 8:58:00 AM
Hey all,
How would you include the actual employees and where they would appear in the tree? Would you need another table or could I add fields to the table presented in this article?

Title: Building a TreeView ResultSet with SQL Server 2005   
Name: Pavel Kubalak
Date: 2007-01-08 12:12:05 PM
Very helpful.
Big thanks
Title: Building a TreeView ResultSet with SQL Server 2005   
Name: Piers Lawson
Date: 2006-10-27 12:23:15 PM
Whilst this new functionality lets you get your result via recurssion, a better solution (that is more efficient, doesn't have the parent NULL issue and works in any database) is to store your heirarchy using Nested Sets as advocated by Joe Celko. One simple query returns all the information you need.
Title: mr   
Name: Pierre Bakker
Date: 2006-10-24 3:42:30 AM

I think you missed an IS NULL on the parentID? If I execute the query in your example it returns all records on all HLevels. I think you need to add a WHERE Clause to the query definition
0 as HLevel
-- extra WHERE
ParentDepartmentID is null.

This way you start at the root of the tree. Am I right?

grtz Pierre Bakker
Title: Building a TreeView ResultSet with SQL Server 2005   
Name: Sanket Terdal
Date: 2006-10-04 8:55:44 AM
Very useful article.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-23 7:32:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search