SQL Server HierarchyId and storing simple web navigation menu
NickName:John Washam Ask DateTime:2013-07-30T05:13:39

SQL Server HierarchyId and storing simple web navigation menu

We would like to store a simple web navigation menu in SQL Server 2012. This will be done for multiple clients, which is why we need it stored. The menu items also need to have a sequence, so they can be ordered how the client wants them. I have been reading up on SQL Server's HierarchyId data type, but almost all the tutorials I find are using the example of an employee or corporate hierarchy, with one root node at the top. After doing hours of reading and testing, I am struck with the feeling that HierarchyId may not the best tool for a simple navigation menu of links. Am I off with this feeling?

The main thing I have noticed about HierarchyId that worries me is that you can only have one root node with HierarchyId. But with a navigation menu, there are obviously multiple top-level "root nodes" that can have children. And since root nodes have no order (they are just "/"), our clients wouldn't be able to move around their top-level menu links.

So the obvious choice would be to have a dummy root node and have all top-level menu links be under that root node. But according to this SO question, marc_s and Jeremy make it seem as if it's unusual (or not according to the normal usage of HierarchyId) to create an artifical "über-root" node, just to have multiple first-level nodes. And by doing this "über-root" node, am I not also throwing off the GetLevel() function of SQL server, since the "top-level" nodes will now display as level 1 instead of 0?

I'm considering just going the route of storing a ParentId in each row and using recursion in C# to build the menu hierarchy. Would I be wrong in doing that? Is the HierarchyId data type really meant for this type of situation, and I am just missing something?

Copyright Notice:Content Author:「John Washam」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17934299/sql-server-hierarchyid-and-storing-simple-web-navigation-menu

More about “SQL Server HierarchyId and storing simple web navigation menu” related questions

SQL Server HierarchyId and storing simple web navigation menu

We would like to store a simple web navigation menu in SQL Server 2012. This will be done for multiple clients, which is why we need it stored. The menu items also need to have a sequence, so they ...

Show Detail

List menu hierarchy using SQL Server hierarchyid

I try to build a menu using the datatype hierarchyid. I have the root node and the current selected node. now I want to list of all elements that are related wetween root and selected node AND there

Show Detail

Sort by hierarchyid in SQL Server

I have a hierarchyid column defined on a table in SQL Server 2008 Let us say that in the first row, the hierarchyid is '/1/7/1/' Let us say that in the second row, the hierarchyid is '/1/10/1/' ...

Show Detail

Differences between SQL Server Hierarchyid and Entity Framework HierarchyId

I am seeing different behavior for the MS SQL Server Datatype hierarchyid, and the corresponding Entity Framework 6 class System.Data.Entity.Hierarchy.HierarchyId (from the assembly EntityFramework...

Show Detail

Updating all HierarchyID nodes in SQL Server 2008

I am importing data from one database to another. The hierarchyid column must also be ported over with the relationships intact. If the target table is empty, it's easy. When the target contains r...

Show Detail

Visual Studio 2015 Preview breaks SQL Server hierarchyid

After installing VS2015, SQL Server can no longer call ToString() method for hierarchyid CLR type - for some values it works, for others it throws the following exception: A .NET Framework error

Show Detail

How is HierarchyId stored in SQL Server?

I have a hierarchyid with the value '/1/'. When I insert it into the SQL Database it's stored as 0x58. Here is my table: CREATE TABLE [dbo].[Category]( [CategoryId] [int] IDENTITY(1,1) NOT NULL...

Show Detail

Some questions about HierarchyId (SQL Server 2008)

I am a newbie in SQL Server 2008 and just got introduced to HierarchyId's. I am learning from SQL Server 2008 - HIERARCHYID - PART I. So basically I am following the article line by line and while

Show Detail

Cannot find data type HIERARCHYID in Sql Server 2012

I have win7, sql server 2012 express, .net Framework 3.5 4.0 4.5. I would like to start tutorial about HierarchyId Data Type but when I try to execute this query, this message arrive : Column, par...

Show Detail

What is the equivalent datatype for SQL Server hierarchyid in PostgreSQL

I have a table in SQL Server which has a column of type hierarchyid, the issue is that I can't find the equivalent datatype for this in PostgreSQL. CREATE TABLE dbo.exampleTable{ id int; name

Show Detail