Modifying the Hierarchy

The script in Listing 45.31 performs the initial population of the Parts_hierarchy table. What if you need to add additional records into the table? Let’s look at how to use the GetDescendant method to add new records at different levels of the hierarchy.

For example, to add a child part to the Body node (node /2/), you can use the GetDescendant method without any arguments to add the new row below Body node at node /2/1/:

INSERT Parts_hierarchy (hid, partid, partname)    select hid.GetDescendant(null, null), 25, 'left front fender'        from Parts_hierarchy        where partname = 'Body'

To add a new row as a higher descendant node at the same level as the left front fender inserted in ...

Get Microsoft® SQL Server 2012 Unleashed 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.