GetAncestor (Motore di database)

Si applica a:SQL Server database SQL di Azure Istanza gestita di SQL di Azure

Restituisce un valore hierarchyid che rappresenta il valore n predecessore di this.

Sintassi

-- Transact-SQL syntax  
child.GetAncestor ( n )   
-- CLR syntax  
SqlHierarchyId GetAncestor ( int n )  

Nota

Per visualizzare la sintassi Transact-SQL per SQL Server 2014 (12.x) e versioni precedenti, vedere la documentazione delle versioni precedenti.

Argomenti

n
Valore int che rappresenta il numero di livelli da salire nella gerarchia.

Tipi restituiti

Tipo SQL Server restituito: hierarchyid

Tipo CLR restituito: SqlHierarchyId

Osservazioni:

Utilizzato per testare se per ogni nodo nell'output il nodo corrente rappresenta un predecessore al livello specificato.

Se viene passato un numero maggiore di GetLevel(), viene restituito NULL.

Se viene passato un numero negativo, viene generata un'eccezione.

Esempi

R. Ricerca dei nodi figlio di un padre

GetAncestor(1) restituisce i dipendenti per cui david0 rappresenta il predecessore immediato (padre). Nell'esempio seguente viene utilizzato GetAncestor(1):

DECLARE @CurrentEmployee hierarchyid  
SELECT @CurrentEmployee = OrgNode FROM HumanResources.EmployeeDemo  
WHERE LoginID = 'adventure-works\david0'  
  
SELECT OrgNode.ToString() AS Text_OrgNode, *  
FROM HumanResources.EmployeeDemo  
WHERE OrgNode.GetAncestor(1) = @CurrentEmployee ;  

B. Restituzione di nipoti di un padre

GetAncestor(2) restituisce i dipendenti che si trovano in una posizione inferiore di due livelli nella gerarchia rispetto al nodo corrente. Tali dipendenti sono i nipoti del nodo corrente. Nell'esempio seguente viene utilizzato GetAncestor(2):

DECLARE @CurrentEmployee hierarchyid  
SELECT @CurrentEmployee = OrgNode FROM HumanResources.EmployeeDemo  
WHERE LoginID = 'adventure-works\ken0'  
  
SELECT OrgNode.ToString() AS Text_OrgNode, *  
FROM HumanResources.EmployeeDemo  
WHERE OrgNode.GetAncestor(2) = @CurrentEmployee ;  

C. Restituzione della riga corrente

Per restituire il nodo corrente usando GetAncestor(0), eseguire il codice seguente.

DECLARE @CurrentEmployee hierarchyid  
SELECT @CurrentEmployee = OrgNode FROM HumanResources.EmployeeDemo  
WHERE LoginID = 'adventure-works\david0'  
  
SELECT OrgNode.ToString() AS Text_OrgNode, *  
FROM HumanResources.EmployeeDemo  
WHERE OrgNode.GetAncestor(0) = @CurrentEmployee ;  

D. Restituzione di un livello della gerarchia se una tabella non è presente

GetAncestor restituisce il livello selezionato della gerarchia anche se una tabella non è presente. Nel codice seguente ad esempio viene specificato un dipendente corrente e viene restituito il valore hierarchyid del predecessore del dipendente corrente senza fare riferimento a una tabella.

DECLARE @CurrentEmployee hierarchyid ;  
DECLARE @TargetEmployee hierarchyid ;  
SELECT @CurrentEmployee = '/2/3/1.2/5/3/' ;  
SELECT @TargetEmployee = @CurrentEmployee.GetAncestor(2) ;  
SELECT @TargetEmployee.ToString(), @TargetEmployee ;  

E. Chiamata a un metodo Common Language Runtime

Nel frammento di codice seguente viene chiamato il metodo GetAncestor().

this.GetAncestor(1)  

Vedi anche

IsDescendantOf (motore di database)
Guida di riferimento ai metodi per il tipo di dati hierarchyid
Dati gerarchici (SQL Server)
hierarchyid (Transact-SQL)