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.

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:

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

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:

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