Index on HierarchyID : Handling Hierarchical data inside the database – Part2

Hi Friends,

Today we will see how we can search ancestors and descendants in a Index on HierarchyID column.  In the previous blog you saw that how can we handle hierarchical data with HierarchyID data type. There we saw creation of table using HierarchyID data type as well as inserting the hierarchical data inside that column. You can go for previous blog;

Now run that select command to see what data is inside the table xthid.

select id.ToString() as id,name from xthid

1_SQL_Server_Handling_Hierarchical_data_inside_the_database_Part2

You can also see this as Tree structure shown below:

2_SQL_Server_Handling_Hierarchical_data_inside_the_database_Part2

Now suppose if you want to select descendant of SQLServer node then you can use IsDescendantOf () method as shown below:

USE HierarchyTest
go
--Descendant Search
Declare @param varchar(20)
Declare @temp1 as HierarchyID
set @param='SQLServer' –For Which you wants to do descendant search
select @temp1=id from xthid where name=@param
select id.ToString() as id, name from xthid where id.IsDescendantOf(@temp1)=1
go

Now if you want to do search for ancestors then you can also use IsDescendantOf () method but in a different way shown below:

USE HierarchyTest
go
--Descendant Search
Declare @param varchar(20)
Declare @temp1 as HierarchyID
set @param='SQLServer' –For Which you wants to do descendant search
select @temp1=id from xthid where name=@param
select id.ToString() as id, name from xthid where id.IsDescendantOf(@temp1)=1
go

3_SQL_Server_Handling_Hierarchical_data_inside_the_database_Part2

Now if you want to do search for ancestors then you can also use IsDescendantOf () method but in a different way shown below:

USE HierarchyTest
go
--Ancestor Search
Declare @param varchar(20)
Declare @temp1 as HierarchyID
set @param='MSOffice2010'
select @temp1=id from xthid where name=@param
select id.ToString() as id, name from xthid where @temp1.IsDescendantOf(id)=1
go

4_SQL_Server_Handling_Hierarchical_data_inside_the_database_Part2

 

Regards

Prince Rastogi

Like us on FaceBook Follow us on Twitter | Join the fastest growing SQL Server group on FaceBook

Follow me on TwitterFollow me on FaceBook

Avatar

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

Leave a Reply

Your email address will not be published. Required fields are marked *