Query for updating a node in a hierarchial tree structure

Rated 3.92/5 based on 562 customer reviews

Hierarchies are sometimes difficult to store in SQL tables...things like trees, threaded forums, org charts and the like..it's usually even harder to retrieve the hierarchy once you do store it.

Here's a method that's easy to understand and maintain, and gives you the full hierarchy (or any piece of it) very quickly and easily.

The nested set model is a particular technique for representing nested sets (also known as trees or hierarchies) in relational databases.

The term was apparently introduced by Joe Celko; others describe the same technique using different terms.

Table 19-1 lists other commonly used tree terms and their descriptions.

When the hierarchy is of fixed or bounded depth, the operations are possible, but expensive, due to the necessity of performing one relational join per level.

This is often known as the bill of materials problem.

Id) WHERE P.pathindex WITH Items(id, parentid, name, depth, pathindex, Item Number) AS ( SELECT id, parentid, name, depth, pathindex, numericalmapping FROM octave_tree ) SELECT * FROM Items ORDER BY CONVERT(hierarchyid, '/' Item Number '/') With the depth and numerical mappings now calculated and saved in the table, we can quickly and easily view the path of a node and its depth.

Only a simple select query is needed each time we need to retrieve the entire tree.

Leave a Reply