GetDescendant(数据库引擎)

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例

返回父级的一个子节点。

语法

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

注意

若要查看 SQL Server 2014 (12.x) 及更早版本的 Transact-SQL 语法,请参阅早期版本文档

自变量

child1
NULL 或当前节点的子节点的 hierarchyid

child2
NULL 或当前节点的子节点的 hierarchyid

返回类型

SQL Server 返回类型:hierarchyid

CLR 返回类型:SqlHierarchyId

备注

返回作为父节点的后代的一个子节点。

  • 如果父级为 NULL,则返回 NULL。
  • 如果父级不为 NULL,而 child1 和 child2 为 NULL,则返回父级的子级。
  • 如果父级和 child1 不为 NULL,而 child2 为 NULL,则返回一个大于 child1 的父级的子级。
  • 如果父级和 child2 不为 NULL,而 child1 为 NULL,则返回一个小于 child2 的父级的子级。
  • 如果父级、child1 和 child2 都不为 NULL,则返回一个大于 child1 且小于 child2 的父级的子级。
  • 如果 child1 不为 NULL 且不是父级的子级,则引发异常。
  • 如果 child2 不为 NULL 且不是父级的子级,则引发异常。
  • 如果 child1 >= child2,则引发异常。

GetDescendant 是确定性的。 因此,如果使用相同的输入调用 GetDescendant,它将始终生成相同的输出。 不过,生成的子级的确切身份可能因其与其他节点的关系而异,如示例 C 所示。

示例

A. 插入一行作为最低级别的后代节点

新雇用了一名员工,该员工将成为位于 /3/1/ 节点的现有员工的下属。 执行以下代码,使用不含参数的 GetDescendant 方法插入新行,以将新行节点指定为 /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 中的经理的下属。执行以下代码,使用带有子级 1 参数的 GetDescendant 方法插入新行,以指定新行的节点将紧跟示例 A 中的节点,成为 /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. 在两个现有节点之间插入一行

雇用了第三名员工,该员工同样将成为示例 A 中的经理的下属。此示例将在比示例 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(),始终可以在任何两个 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)