Click to Rate and Give Feedback
MSDN
MSDN Library
SQL Server
SQL Server 2008
Database Engine
Technical Reference
 IsDescendantOf (Database Engine)
Community Content
In this section
Statistics Annotations (3)
Collapse All/Expand All Collapse All
SQL Server 2008 Books Online (October 2009)
IsDescendantOf (Database Engine)

Returns true if this is a descendant ofparent.

-- Transact-SQL syntax
child.IsDescendantOf ( parent )
-- CLR syntax
SqlHierarchyId IsDescendantOf ( SqlHierarchyId parent ) 
parent

The hierarchyid node for which the IsDescendantOf test should be performed.

SQL Server return type: bit

CLR return type: SqlBoolean

Returns true for all the nodes in the sub-tree rooted at parent, and false for all other nodes.

Parent is considered its own descendant.

A. Using IsDescendantOf in a WHERE clause

The following example returns a manager and the employees that report to the manager:

DECLARE @Manager hierarchyid
SELECT @Manager = OrgNode FROM HumanResources.EmployeeDemo
  WHERE LoginID = 'adventure-works\dylan0'

SELECT * FROM HumanResources.EmployeeDemo
WHERE @OrgNode.IsDescendantOf(Manager) = 1

B. Using IsDescendantOf to evaluate a relationship

The following code declares and populates three variables. It then evaluates the hierarchical relationship and returns one of two printed results based on the comparison:

DECLARE @Manager hierarchyid, @Employee hierarchyid, @LoginID nvarchar(256)
SELECT @Manager = OrgNode FROM HumanResources.EmployeeDemo
  WHERE LoginID = 'adventure-works\terri0' ;

SELECT @Employee = OrgNode, @LoginID = LoginID FROM HumanResources.EmployeeDemo
  WHERE LoginID = 'adventure-works\rob0'

IF @Employee.IsDescendantOf(@Manager) = 1
   BEGIN
    PRINT 'LoginID ' + @LoginID + ' is a subordinate of the selected Manager.'
   END
ELSE
   BEGIN
    PRINT 'LoginID ' + @LoginID + ' is not a subordinate of the selected Manager.' ;
   END

C. Calling a common language runtime method

The following code snippet calls the IsDescendantOf() method.

this.IsDescendantOf(Parent)
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Don't you think...      Smarty   |   Edit   |   Show History
...that child.IsDescendantOf ( parent ) is more correct formal syntax for this method??
Tags What's this?: Add a tag
Flag as ContentBug
doc error      tanoshimi   |   Edit   |   Show History
I think that is actual behaviour - the documentation above is incorrect:

DECLARE @parent hierarchyid = '/';
DECLARE @child hierarchyid = '/1/';
SELECT @child.IsDescendantOf(@parent);
---
result: 1
Tags What's this?: Add a tag
Flag as ContentBug
another doc error      NMyVision   |   Edit   |   Show History
the last line of code in Section A should read...

WHERE OrgNode.IsDescendantOf(@Manager) = 1


Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker