companyId   name                 parentCompanyId----------- -------------------- ---------------1           Company HQ           NULL2           Maine HQ             13           Tennessee HQ         14           Nashville Branch     35           Knoxville Branch     36           Memphis Branch       37           Portland Branch      28           Camden Branch        2

You can traverse the data from child to parent fairly easy using the key structures. In the next code, we will write a query to get the children of a given node and add a column to the output that shows the hierarchy. I have commented the code to show what I was doing, but it is fairly straightforward how this code works once you have wrapped your head around it a few times. Recursive ...

Get Pro SQL Server Relational Database Design and Implementation, Fifth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.