Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

HierarchyID for Multiple Root Nodes Expand / Collapse
Author
Message
Posted Tuesday, February 10, 2009 12:07 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 5:16 AM
Points: 1,090, Visits: 388
Hi,
I have the following scenario for which I am trying to implement the hierarchyid datatype:
Have an organization table which can have multiple root nodes and subsequent child nodes.
How do I proceed with such a scenario?

For Example:

Organization ParentOrganization
1 NULL
2 NULL
3 1
4 2
5 2
6 3
7 4

When I try using the hierarchyid::GetRoot for populating the organisations 1 and 2 I get the same hierarchyid and so I am unable to proceed.
Please suggest to proceed
Report post as abusive
Post #653440
Posted Wednesday, February 18, 2009 8:12 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, October 17, 2014 9:04 AM
Points: 202, Visits: 1,101
It seems to me that you need a higher level here. There should only be a single "Root". In this case you are trying to have two different roots. I may be wrong but it seems that thinking of a Hierarchy lilke a single tree with many branches, you must have a single root or essentially have different trees all together (separate table structure). The two organizations that you are trying to make as the root, I assume that they correlate somehow to one overarching "organization"? If so that should be the single root, and each of the two that you have now as the root should be the second level of the Hierarchy.
Post #659438
Posted Wednesday, February 25, 2009 6:02 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 5:16 AM
Points: 1,090, Visits: 388
Higher Level may not be a solution as per the functionallity .
Post #664160
Posted Wednesday, February 25, 2009 6:57 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, October 17, 2014 9:04 AM
Points: 202, Visits: 1,101
Any possibility of two different table structures that you could bring together through views?

Or perhaps you could create your own hierarchy structure that essentially works in the same fashion as the one in SQL 2008 but gives you the ability to have multiple roots.
Post #664206
Posted Thursday, December 3, 2009 12:37 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 10, 2013 12:55 PM
Points: 28, Visits: 124
Have you tried using a multi-field Primary Key?
Post #828437
Posted Thursday, December 3, 2009 1:07 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, October 17, 2014 2:18 PM
Points: 2,039, Visits: 3,485
Not sure whether or not this is applicable, but this sounds similar to the scenario presented in an article I read here a few weeks ago.

Here's the link to the article.

Hope this helps!
Post #828467
Posted Thursday, December 3, 2009 9:40 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, June 13, 2012 5:16 AM
Points: 1,090, Visits: 388
Ray K (12/3/2009)
Not sure whether or not this is applicable, but this sounds similar to the scenario presented in an article I read here a few weeks ago.

Here's the link to the article.

Hope this helps!


Thanks for the article.
This will be applicable when there is a definite number of known levels like parents.Levels that i may have to traverse is not specific, also cross apply is an approach to operate on the exisitng data model where in we have all the parent and child in the same table and a column to differentiate between them.Rather i want to use the datatype hierarchyid as it has its own advantages as easier access to nodes with specific functions and mainly memory management.
Post #828632
Posted Friday, March 5, 2010 9:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 29, 2014 11:47 AM
Points: 3, Visits: 118
It sounds like you need to a "dummy" root node. You can then have your multiple roots be children of that one. The "dummy" root would not contain any actual info but just be used as the single root that is required. Your queries would need to exclude the "dummy" from being returned but it would be easy to determine that by using the GetRoot() method.
Post #877725
Posted Monday, January 9, 2012 7:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 2:57 AM
Points: 1, Visits: 7
I don't understand.. Can't you just use Hierarchy combined with integer as the primary key?

For example, I'm using ChecklistID and ChecklistItemID in this example in order to store multiple parent child hierarchies.. in the same table.

CREATE TABLE [dbo].[ChecklistItems](
[ChecklistID] [int] NOT NULL,
[ChecklistItemID] [int] IDENTITY(1001,1) NOT NULL,
[ChecklistItemName] [varchar](50) NOT NULL,
[ChecklistItemParent] [int] NOT NULL,
[OrderBy] [smallint] NOT NULL,
[ChildLevel] [tinyint] NOT NULL,
[ChildLevelDesc] [tinyint] NOT NULL,
[ChecklistHierarchy] [hierarchyid] NULL,
[HtmlPrefix] [text] NULL,
[HtmlSuffix] [text] NULL,
CONSTRAINT [PK_ChecklistParentChild] PRIMARY KEY CLUSTERED
(
[ChecklistID] ASC,
[ChecklistItemID] ASC
)
)

Now I can easily generate a checklist hierarchy when I pass in a single checklistID into this sproc spCreateChecklist

CREATE procedure [dbo].[spCreateChecklist]
(
@checklistID int
)
as

with H(ChecklistID, ChecklistItemID, Level, ChecklistItemName, ChecklistItemParent, FQName)
As
(
Select ChecklistID, ChecklistItemID, 0, ChecklistItemName, ChecklistItemParent, Convert(varchar(max), ChecklistItemName)
From checklistparentchild
Where checklistItemID = checklistItemParent
and ChecklistID = @checklistID
UNION ALL

Select C.ChecklistID, C.ChecklistItemID, H.Level + 1, C.ChecklistItemName, C.ChecklistItemParent, Convert(varchar(max), H.FQName) + '.' + Convert(varchar(max), C.ChecklistItemName)
From checklistparentchild C
INNER JOIN H on H.checklistItemID = C.checklistItemParent
Where C.checklistItemID != C.checklistItemParent
and C.ChecklistID = @checklistID
)


Select *, SPACE(Level*3) + ChecklistItemName as IndentedName
From H
Order by FQName

Using Views, I think that this will be quite straight forward to navigate / flatten... of course, I've been using views to flatten parent-childs for a decade.

I just don't understand why you haven't considered this as an option

I'm at this post because I'm trying to change ChecklistItemID to ChecklistHierarchy in this example, I haven't been able to get my head around what you're having difficulty with, sorry.

-Aaron
MCITP: DBA SQL Server

Post #1232423
Posted Thursday, January 23, 2014 11:41 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 12:19 AM
Points: 8, Visits: 121
A nice solution for traversing hierarchies with nodes having multiple parents.
Post #1534355
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse