Search code examples
sql-serverparenthierarchyid

Get the parent SQL Server variable hierarchyid


How to get the parent node in a table using the variable of the variable of type hierarchyid in sql by id(EmployeeID)? this is my table

CREATE TABLE Employee
(
   Node hierarchyid PRIMARY KEY CLUSTERED,
   EmployeeID int UNIQUE NOT NULL,
   EmpName varchar(20) NOT NULL,
   Title varchar(20) NULL
) ;
GO

Solution

  • I find a simple way to solve my problem:

    SELECT EmployeeID 
    FROM Employee
    WHERE [Node] IN (
                   SELECT [Node].GetAncestor(1).ToString()
                   FROM Employee
                   WHERE EmployeeID=4
                   )
    

    thanks for your answer!!!