|
|
|
Ten 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
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 2:07 PM
Points: 197,
Visits: 1,028
|
|
| 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.
|
|
|
|
|
Ten 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 .
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 2:07 PM
Points: 197,
Visits: 1,028
|
|
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.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 7:22 PM
Points: 27,
Visits: 120
|
|
Have you tried using a multi-field Primary Key?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 6:25 PM
Points: 2,062,
Visits: 3,221
|
|
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!
|
|
|
|
|
Ten 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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: 2 days ago @ 2:32 PM
Points: 2,
Visits: 53
|
|
| 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.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 03, 2012 10:43 AM
Points: 1,
Visits: 6
|
|
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
|
|
|
|