关于HierarchyId (SQL Server 2008)的一些问题
我是 SQL Server 2008 的新手,刚刚接触 HierarchyId。
我正在学习 SQL Server 2008 - HIERARCHYID - 第一部分。所以基本上我是逐行关注这篇文章的,在 SSMS 中练习时,我发现对于每个 ChildId 都会生成一些十六进制值,如 0x、0x58、0x5AC0 等。
我的问题是
- 这些十六进制值是什么?
- 为什么会生成这些以及它们的用途是什么?我的意思是我可以在哪里使用这些十六进制值?
- 我们可以控制这些十六进制值吗?我的意思是我们可以更新等等。
- 如何通过查看这些十六进制值来确定层次结构。我的意思是如何确定哪个是父级,哪个是子级?
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
- What are these hexadecimal values?
- Why are these generated and what is their use? I mean where can I use those hexa values?
- Do we have any control over those hexa values? I mean can we update etc.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些十六进制值只是层次结构级别的二进制表示。一般来说,您不应该直接使用它们。
您可能想查看以下示例,我认为它应该是不言自明的。我希望它能让你朝着正确的方向前进。
创建一个包含
hierarchyid
字段的表:插入一些值:
查询表:
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:Insert some values:
Query the table:
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
我会让其他人解决您的具体问题,但我会告诉您,在我看来,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