gpt4 book ai didi

sql - 如何使用 SQL Server 的 HierarchyID 查找所有后代

转载 作者:行者123 更新时间:2023-12-03 22:51:05 24 4
gpt4 key购买 nike

我需要使用 HierarchyID 找到一个类别的所有后代为 SQL Server .

我知道如何找到直接的 child ,但我想找到 child 的 child 的 child 等等。

有没有办法使用 HierarchyID 来做到这一点? ?

最佳答案

如果你有你想要的树的根,你不能只使用:

DECLARE @root hierarchyID;

SELECT @root = col
FROM yourTable
WHERE [whatever uniquely identifies this row]

SELECT *
FROM yourTable
WHERE col.IsDescendantOf(@root) = 1

关于sql - 如何使用 SQL Server 的 HierarchyID 查找所有后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com