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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.