关于HierarchyId (SQL Server 2008)的一些问题

发布于 2024-08-14 11:43:03 字数 478 浏览 2 评论 0原文

我是 SQL Server 2008 的新手,刚刚接触 HierarchyId。

我正在学习 SQL Server 2008 - HIERARCHYID - 第一部分。所以基本上我是逐行关注这篇文章的,在 SSMS 中练习时,我发现对于每个 ChildId 都会生成一些十六进制值,如 0x、0x58、0x5AC0 等。

我的问题是

  1. 这些十六进制值是什么?
  2. 为什么会生成这些以及它们的用途是什么?我的意思是我可以在哪里使用这些十六进制值?
  3. 我们可以控制这些十六进制值吗?我的意思是我们可以更新等等。
  4. 如何通过查看这些十六进制值来确定层次结构。我的意思是如何确定哪个是父级,哪个是子级?

I am a newbie in SQL Server 2008 and just got introduced to HierarchyId's.

I am learning from SQL Server 2008 - HIERARCHYID - PART I. So basically I am following the article line by line and while practicing in SSMS I found that for every ChildId some hexadecimal values are generated like 0x,0x58,0x5AC0 etc.

My questions are

  1. What are these hexadecimal values?
  2. Why are these generated and what is their use? I mean where can I use those hexa values?
  3. Do we have any control over those hexa values? I mean can we update etc.
  4. How to determine the hierarchy by looking into those hexa values.. I mean how can I determine which is the parent and which is the child?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风筝有风,海豚有海 2024-08-21 11:43:03

这些十六进制值只是层次结构级别的二进制表示。一般来说,您不应该直接使用它们。

您可能想查看以下示例,我认为它应该是不言自明的。我希望它能让你朝着正确的方向前进。

创建一个包含 hierarchyid 字段的表:

CREATE TABLE groups (
    group_name       nvarchar(100)  NOT NULL,
    group_hierarchy  hierarchyid    NOT NULL
);

插入一些值:

INSERT INTO groups (group_name, group_hierarchy)
VALUES
    ('root',     hierarchyid::Parse('/')),
    ('domain-a', hierarchyid::Parse('/1/')),
    ('domain-b', hierarchyid::Parse('/2/')),
    ('sub-a-1',  hierarchyid::Parse('/1/1/')),
    ('sub-a-2',  hierarchyid::Parse('/1/2/'));

查询表:

SELECT 
    group_name,
    group_hierarchy.ToString()
FROM
    groups
WHERE
    (group_hierarchy.IsDescendantOf(hierarchyid::Parse('/1/')) = 1);

Those hex values are simply a binary representation of the hierarchy level. In general, you should not use them directly.

You may want to check out the following example, which I think should be self-explanatory. I hope it will get you going in the right direction.

Create a table with a hierarchyid field:

CREATE TABLE groups (
    group_name       nvarchar(100)  NOT NULL,
    group_hierarchy  hierarchyid    NOT NULL
);

Insert some values:

INSERT INTO groups (group_name, group_hierarchy)
VALUES
    ('root',     hierarchyid::Parse('/')),
    ('domain-a', hierarchyid::Parse('/1/')),
    ('domain-b', hierarchyid::Parse('/2/')),
    ('sub-a-1',  hierarchyid::Parse('/1/1/')),
    ('sub-a-2',  hierarchyid::Parse('/1/2/'));

Query the table:

SELECT 
    group_name,
    group_hierarchy.ToString()
FROM
    groups
WHERE
    (group_hierarchy.IsDescendantOf(hierarchyid::Parse('/1/')) = 1);
最丧也最甜 2024-08-21 11:43:03

Adam Milazzo 在这里写了一篇关于 Hierarchyid 内部结构的精彩文章:

http://www. adamamil.net/blog/view.php?id=100

简而言之,处理直接十六进制的东西没有意义,而是将数字转换为二进制。原因是事物并没有在字节边界上被分割。如果单个节点是前四个节点之一,则表示它可以短至 5 位。随着使用的节点越来越多,变得越来越长,接下来的4个节点每个6位,接下来的8个节点每个7位,然后跳到接下来的64个节点每个12位!然后接下来的 1024 每个最多 18 位。

我需要将数据库转换为 Postgres,并编写一个脚本来解析这些十六进制值。您可以在此处查看我为 AdventureWorks 制作的版本,搜索“hierarchyid”:

https://github.com/lorint/AdventureWorks-for-Postgres/blob/master/install.sql

Adam Milazzo wrote a great article about the innards of hierarchyid here:

http://www.adammil.net/blog/view.php?id=100

In a nutshell, it's not meaningful to work with things in straight hex, but rather convert the numbers out to binary. The reason is that things are not cut up on even byte boundaries. Representing a single node can be as short as 5 bits if it's one of the first four nodes. Becomes longer and longer as more nodes are used, 6 bits each for the next 4 nodes, 7 bits each for the next 8 nodes, and then it jumps to 12 bits each for the next 64 nodes! And then up to 18 bits each for the next 1024.

I needed to convert a database to Postgres, and wrote a script which parses these hex values. You can check out a version I made for AdventureWorks here, search for "hierarchyid":

https://github.com/lorint/AdventureWorks-for-Postgres/blob/master/install.sql

请你别敷衍 2024-08-21 11:43:03

我会让其他人解决您的具体问题,但我会告诉您,在我看来,SQL Server 2008 中的 HierarchyId 并不是 Microsoft 对 SQL Server 的最大贡献之一。它们很复杂,而且有些尴尬。我想您会发现,对于许多分层需求,公用表表达式 (CTE) 效果很好。

兰迪

I'll let others address your specific questions, but I will tell you, that, IMO, the HierarchyId in SQL Server 2008 isn't one of Microsoft's greatest contributions to SQL Server. They are complex and somewhat awkward. I think you will find that for many hierarchical needs, common table expressions (CTE) work great.

Randy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文