GetDescendant (データベース エンジン)

適用対象:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

親の子ノードを返します。

構文

-- Transact-SQL syntax  
parent.GetDescendant ( child1 , child2 )   
-- CLR syntax  
SqlHierarchyId GetDescendant ( SqlHierarchyId child1 , SqlHierarchyId child2 )   

Note

SQL Server 2014 (12.x) 以前のバージョンの Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。

引数

child1
NULL または現在のノードの子の hierarchyid

child2
NULL または現在のノードの子の hierarchyid

戻り値の型

SQL Server の戻り値の型: hierarchyid

CLR 戻り値の型:SqlHierarchyId

解説

親の子孫である 1 つの子ノードを返します。

  • parent が NULL の場合、NULL を返します。
  • parent が NULL でなく、child1 と child2 の両方が NULL の場合、parent の子を返します。
  • parent と child1 が NULL でなく、child2 が NULL の場合、child1 より大きい parent の子を返します。
  • parent と child2 が NULL でなく、child1 が NULL の場合、child2 より小さい parent の子を返します。
  • parent、child1、child2 が NULL でない場合、child1 より大きく child2 より小さい parent の子を返します。
  • child1 が NULL でなく parent の子でない場合、例外が発生します。
  • child2 が NULL でなく parent の子でない場合、例外が発生します。
  • child1 >= child2 の場合は、例外が発生します。

GetDescendant は決定的です。 そのため場合、 GetDescendant と呼ばれるは、同じ入力でが同じ出力常に生成されます。 ただし、生成された子の正確な ID は、例 C に示されているように、他のノードとの関係によって異なります。

A. 最も小さい子孫ノードとして行を挿入

ノード /3/1/ の既存の従業員の部下として新しい従業員が採用されました。 使用して、新しい行を挿入するには、次のコードの実行、 GetDescendant メソッドとして、新しい行のノードを指定する引数を指定しないで /3 1/1/:/3/1/1/

DECLARE @Manager hierarchyid;   
SET @Manager = CAST('/3/1/' AS hierarchyid);  
  
INSERT HumanResources.EmployeeDemo (OrgNode, LoginID, Title, HireDate)  
VALUES  
(@Manager.GetDescendant(NULL, NULL),  
'adventure-works\FirstNewEmployee', 'Application Intern', '3/11/07') ;  

B. 大きい子孫ノードとして行を挿入

別の新しい従業員を雇用すると、次のコードを使用して、新しい行を挿入することを A. の実行例と同じ上司に報告、 GetDescendant メソッドは、child 1 引数を使用して、新しい行のノードが例 A でノードを従うことを指定するになる /3 1/2/:/3/1/2/

DECLARE @Manager hierarchyid, @Child1 hierarchyid;  
  
SET @Manager = CAST('/3/1/' AS hierarchyid);  
SET @Child1 = CAST('/3/1/1/' AS hierarchyid);  
  
INSERT HumanResources.EmployeeDemo (OrgNode, LoginID, Title, HireDate)  
VALUES  
(@Manager.GetDescendant(@Child1, NULL),  
'adventure-works\SecondNewEmployee', 'Application Intern', '3/11/07') ;  

C. 既存の 2 つのノードの間に行を挿入

例 A と同じ上司の部下として 3 人目の従業員が採用されました。この例では、例 A の FirstNewEmployee より大きく例 B の SecondNewEmployee より小さいノードに新しい行を挿入します。GetDescendant メソッドを使用して次のコードを実行します。 このコードでは、child1 引数と child2 引数の両方を使用して、新しい行のノードがノード /3/1/1.1/ になるように指定しています。

DECLARE @Manager hierarchyid, @Child1 hierarchyid, @Child2 hierarchyid;  
  
SET @Manager = CAST('/3/1/' AS hierarchyid);  
SET @Child1 = CAST('/3/1/1/' AS hierarchyid);  
SET @Child2 = CAST('/3/1/2/' AS hierarchyid);  
  
INSERT HumanResources.EmployeeDemo (OrgNode, LoginID, Title, HireDate)  
VALUES  
(@Manager.GetDescendant(@Child1, @Child2),  
'adventure-works\ThirdNewEmployee', 'Application Intern', '3/11/07') ;  
  

A、B、および C# の例を完了すると、テーブルに追加されたノード ピアになります次 hierarchyid 値。

/3/1/1/

/3/1/1.1/

/3/1/2/

ノード /3/1/1.1/ は、ノード /3/1/1/ より大きいノードですが、階層のレベルは同じです。

D. スカラーの例

SQL Server では、すべての hierarchyid ノードの任意の挿入および削除がサポートされています。 使用して GetDescendant(), 、任意の 2 つの間のノードを生成することは常に hierarchyid ノードです。 次のコードを実行すると、GetDescendant を使用してサンプル ノードが生成されます。

DECLARE @h hierarchyid = hierarchyid::GetRoot();  
DECLARE @c hierarchyid = @h.GetDescendant(NULL, NULL);  
SELECT @c.ToString();  
DECLARE @c2 hierarchyid = @h.GetDescendant(@c, NULL);  
SELECT @c2.ToString();  
SET @c2 = @h.GetDescendant(@c, @c2);  
SELECT @c2.ToString();  
SET @c = @h.GetDescendant(@c, @c2);  
SELECT @c.ToString();  
SET @c2 = @h.GetDescendant(@c, @c2);  
SELECT @c2.ToString();  

E. CLR の例

次のコード例では GetDescendant() メソッドを呼び出します。

SqlHierarchyId parent, child1, child2;  
parent = SqlHierarchyId.GetRoot();  
child1 = parent.GetDescendant(SqlHierarchyId.Null, SqlHierarchyId.Null);  
child2 = parent.GetDescendant(child1, SqlHierarchyId.Null);  
Console.Write(parent.GetDescendant(child1, child2).ToString());  

参照

hierarchyid データ型メソッド リファレンス
階層データ (SQL Server)
hierarchyid (Transact-SQL)