Ho eseguito alcuni test e sembra che non sia necessario un record con una gerarchia di root.
Per esempio, normalmente si avrebbe un nodo radice (livello 1) e più infantile, ma si può saltare il nodo principale, non avendo record di root, solo i record che iniziano a livello 2:
//table schema
CREATE TABLE [Entity](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL
[Hierarchy] [hierarchyid] NOT NULL,
CONSTRAINT [PK_Entity] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
//Insert first 'root', which is technicall a child without a parent
INSERT INTO [Entity]
([Name]
,[Description]
,[Hierarchy])
VALUES
('Root A'
,hierarchyid::GetRoot().GetDescendant(NULL,NULL))
//Create the second 'root'
INSERT INTO [Entity]
([Name]
,[Hierarchy])
VALUES
('Root B'
,hierarchyid::GetRoot().GetDescendant((select MAX(hierarchy) from entity where hierarchy.GetAncestor(1) = hierarchyid::GetRoot()),NULL))
Ora se si seleziona tutte le righe della tabella, si vede:
SELECT [ID]
,[Name]
,[Hierarchy],
[Hierarchy].ToString()
FROM [Entity]
ID Nome Gerarchia (No nome della colonna)
Root Un 0x58 /1/
Root B 0x68 /2/
Non sono sicuro se questo sarebbe raccomandato pratica, ma concettualmente si permette di avere radici multiple, fino a quando si considerare il 2 ° livello nell'albero come radice
Questo non è corretto. Vedi la mia [risposta] (http://stackoverflow.com/a/23290250/81595) per i dettagli. –
@ScottMunro: vedere il mio commento per la risposta. Credo che Marc abbia ragione. Tuttavia, sei libero di utilizzare i figli di root come root-node – Phil
@Phil, in realtà non è necessario avere un nodo "uber-root". Puoi iniziare una gerarchia con un nodo come '/ 1 /' e avere un secondo come '/ 2 /' senza dover avere '/' presente sopra di loro. –