1 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
CPOL
Introduction
Here in this post we will try to manage datas with hierarchical relation or parent-child relation of a
specific table in SQL server. Our special concentration would be over,
Show Generations of each row
Find all possible parents of a specific row
Find all possible childs of a specific row
Show all possible parents at a column with a separator
Show all possible childs at a column with a separator
Background
Lets pretend
A row can have only one parent or Null as no parent
There is at least a row, without parent (parentId is null)
And at least a row, without any child
Here is our table schema.
/*drop the tbl*/
--DROP TABLE UserType
/*create the tbl*/
CREATE TABLE UserType(
Id BIGINT NOT NULL,
6/11/2015 12:55 AM
2 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
6/11/2015 12:55 AM
3 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
Regular Join
Joining table with itself based on condition, where ones parentId is equal to anothers Id
/*regular join to get detail*/
SELECT ChildUserType.Id, ChildUserType.Name, ParentUserType.Id,
ParentUserType.Name
FROM UserType AS ChildUserType
LEFT JOIN UserType AS ParentUserType ON ChildUserType.ParentId =
ParentUserType.Id;
Row Generation
The procedure is something like
All rows with no parent (NULL), assign generation 0 to them
Find rows where parent belongs to the generation 0, and assign increased generation to itself
Do until the recursion is finished.
/*row generations*/
WITH Hierarchy(ChildId, ChildName, Generation, ParentId)
AS
(
SELECT Id, Name, 0, ParentId
FROM UserType AS FirtGeneration
WHERE ParentId IS NULL
UNION ALL
SELECT NextGeneration.Id, NextGeneration.Name, Parent.Generation + 1,
Parent.ChildId
FROM UserType AS NextGeneration
INNER JOIN Hierarchy AS Parent ON NextGeneration.ParentId =
Parent.ChildId
)
SELECT *
FROM Hierarchy
OPTION(MAXRECURSION 32767)
6/11/2015 12:55 AM
4 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
ON UserType.Id = tblParent.ParentId
6/11/2015 12:55 AM
5 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
ON UserType.ParentId =
6/11/2015 12:55 AM
6 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
6/11/2015 12:55 AM
7 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
Limitations
Yes, there could be something which I misunderstood or presented. So if you find anything, just let
me know.
Find the SQL file at the attachment.
License
This article, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)
Share
About the Author
6/11/2015 12:55 AM
8 of 8
http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hi...
DiponRoy
Bangladesh
No Biography provided
Requirements
Management for the
Internet of Things
Parent/Child
(Hierarchical)
Relational Treeview
User Control
Development in
ASP.NET
SAPrefs Netscape-like
Preferences Dialog
Select Language
6/11/2015 12:55 AM