﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / HierarchyID for Multiple Root Nodes / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 19:05:17 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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) aswith H(ChecklistID, ChecklistItemID, Level, ChecklistItemName, ChecklistItemParent, FQName)As(Select ChecklistID, ChecklistItemID, 0, ChecklistItemName, ChecklistItemParent, Convert(varchar(max), ChecklistItemName)From checklistparentchildWhere checklistItemID = checklistItemParentand ChecklistID = @checklistID UNION ALLSelect C.ChecklistID, C.ChecklistItemID, H.Level + 1, C.ChecklistItemName, C.ChecklistItemParent, Convert(varchar(max), H.FQName) + '.' + Convert(varchar(max), C.ChecklistItemName)From checklistparentchild CINNER JOIN H on H.checklistItemID = C.checklistItemParentWhere C.checklistItemID != C.checklistItemParentand C.ChecklistID = @checklistID )Select *, SPACE(Level*3) + ChecklistItemName as IndentedNameFrom HOrder by FQNameUsing 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 optionI'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.-AaronMCITP: DBA SQL Server</description><pubDate>Mon, 09 Jan 2012 07:41:24 GMT</pubDate><dc:creator>aaron.kempf 89506</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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.</description><pubDate>Fri, 05 Mar 2010 09:07:41 GMT</pubDate><dc:creator>kevin.kraemer</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>[quote][b]Ray K (12/3/2009)[/b][hr]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 [url=http://www.developer.com/db/article.php/3798361/Using-T-SQL-CROSS-APPLY-and-OUTER-APPLY.htm]link to the article[/url].Hope this helps![/quote]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. </description><pubDate>Thu, 03 Dec 2009 21:40:12 GMT</pubDate><dc:creator>Gayathri.Varadarajan</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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 [url=http://www.developer.com/db/article.php/3798361/Using-T-SQL-CROSS-APPLY-and-OUTER-APPLY.htm]link to the article[/url].Hope this helps!</description><pubDate>Thu, 03 Dec 2009 13:07:30 GMT</pubDate><dc:creator>Ray K</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>Have you tried using a multi-field Primary Key?</description><pubDate>Thu, 03 Dec 2009 12:37:40 GMT</pubDate><dc:creator>Micah Ritchie</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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.</description><pubDate>Wed, 25 Feb 2009 06:57:29 GMT</pubDate><dc:creator>Mike - CI</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>Higher Level may not be a solution as per the functionallity .</description><pubDate>Wed, 25 Feb 2009 06:02:37 GMT</pubDate><dc:creator>Gayathri.Varadarajan</dc:creator></item><item><title>RE: HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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.</description><pubDate>Wed, 18 Feb 2009 08:12:01 GMT</pubDate><dc:creator>Mike - CI</dc:creator></item><item><title>HierarchyID for Multiple Root Nodes</title><link>http://www.sqlservercentral.com/Forums/Topic653440-392-1.aspx</link><description>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                        NULL2                        NULL3                        14                        25                        26                        37                        4When 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</description><pubDate>Tue, 10 Feb 2009 00:07:03 GMT</pubDate><dc:creator>Gayathri.Varadarajan</dc:creator></item></channel></rss>