There are roughly three different approaches to store and retrieve hierarchical data in SQL Server:
Parent-Child-Approach with Recursive TCE:
You can save the ID of a node's parent in an extra “parent column”:
Then you connect the referenced items with recursive Common Table Expressions (CTE).
HierarchyID data type:
Or you save the node’s hierarchy location with the new data type HierarchyID, which stores a path to a node and, thus, reveals the where in a tree the row occurs. A sample table looks like this:
The hierarchy is directly visible and, unlike in the simple parent-child-pattern, you have now the HierarchyID methods to work against the tree structure.
XML data type:
The third option is to represent the hierarchy in a XML document and store the instance in an xml column:
Doing this, you can retrieve information from the tree using XQuery.
This is meant to be a teaser... ;-) I'm going to write more about this the next days.
No comments:
Post a Comment