当我尝试查找单个父节点时,此代码不起作用
declare @val hierarchyid
select @val = dbo.GetHierarchyIDbyID(30)
--get the parent
select * from NodeHierarchy
where @val.GetAncestor(NodeHierarchyID) = 1
你会如何寻找父母?
答案 0 :(得分:6)
假设包含层次结构ID的字段名称为“hid”,您可以执行以下操作:
DECLARE @val HIERARCHYID
SELECT @val = dbo.GetHierarchyIDbyID(30)
SELECT * FROM NodeHierarchy WHERE hid = @val.GetAncestor(1)