Index on HierarchyID : Handling Hierarchical data inside the database Part1

Sometimes we need to handle hierarchical data in databases like trees where data elements are having relationship like parent and child. Before SQL Server 2008 it was complicated to deal with such kind of data. Inside SQL Server 2008 Microsoft introduces a new data type  Index on HierarchyID to handle such type of data and reduce the complexity. By using this data type you can also use various methods provided by SQL Server to deal with such data. Here we will see how can, we use it:

Now insert first data row with below code:

Now if you want to see the data inside that table then


Let’s insert some more data:

Now just Select the data from above table:


Here the output of id column is not clear, so we can use ToString() method on id column here to make the output as more readable and understandable:


Here the above output is clearer.



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