SQL Server 2005 中使用 XML 的层次结构
我想知道是否有办法在 SQL Server 2005 中选择层次结构并返回 xml 格式? 我有一个包含大量数据的数据库(大约 2000 到 3000 条记录),我现在使用 SQL Server 2005 中的一个函数来检索层次结构中的数据并返回 XML,但它似乎并不完美,因为当有大量数据
这是我的函数
数据库
ID Name Parent Order
函数
CREATE FUNCTION [dbo].[GetXMLTree]
(
@PARENT bigint
)
RETURNS XML
AS
BEGIN
RETURN /* value */
(SELECT [ID] AS "@ID",
[Name] AS "@Name",
[Parent] AS "@Parent",
[Order] AS "@Order",
dbo.GetXMLTree(Parent).query('/xml/item')
FROM MyDatabaseTable
WHERE [Parent]=@PARENT
ORDER BY [Order]
FOR XML PATH('item'),ROOT('xml'),TYPE)
END
我想在层次结构中使用 XML,因为对我来说,有很多事情需要处理:) 任何最好的解决方案plzzzzz
I wonder is there anyway to select hierarchy in SQL server 2005 and return xml format?
I have a database with a lot of data (about 2000 to 3000 records), and i am now using a function in SQL server 2005 to retrieve the data in hierarchy and return an XML but it seems not perfect because it's too slow when there is a lot of data
Here is my function
Database
ID Name Parent Order
Function
CREATE FUNCTION [dbo].[GetXMLTree]
(
@PARENT bigint
)
RETURNS XML
AS
BEGIN
RETURN /* value */
(SELECT [ID] AS "@ID",
[Name] AS "@Name",
[Parent] AS "@Parent",
[Order] AS "@Order",
dbo.GetXMLTree(Parent).query('/xml/item')
FROM MyDatabaseTable
WHERE [Parent]=@PARENT
ORDER BY [Order]
FOR XML PATH('item'),ROOT('xml'),TYPE)
END
I would like to use XML in hierarchy because with me there's alot of thing to do with it :)
Any best solutions plzzzzz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用递归 CTE 来构建层次结构并在各个级别之间循环来构建 XML。
结果
You can use a recursive CTE to build the hierarchy and loop over levels to build the XML.
Result
您期望使用 XML 有什么好处?当您无论如何都需要 XML 时,我没有一个完美的解决方案 - 但也许您也可以研究替代方案?
使用递归 CTE(通用表表达式),您可以轻松地在单个结果集中获取整个层次结构,并且性能应该明显优于递归 XML 构建函数。
检查此 CTE:
这将为您提供一个结果集,其中返回所有行,按级别排序(1 表示根级别,每个下一级增加 1)及其
[Order]
列。这可以成为你的替代方案吗?是不是性能比较好??
What benefit do you expect from using XML? I don't have a perfect solution for the case when you need XML by all means - but maybe you could also investigate alternatives??
With a recursive CTE (Common Table Expression), you could easily get your entire hierarchy in a single result set, and performance should be noticeably better than doing a recursive XML building function.
Check this CTE out:
This gives you a single result set, where all rows are returned, ordered by level (1 for the root level, increasing 1 for each down level) and their
[Order]
column.Could that be an alternative for you? Does it perform better??
我意识到这个答案有点晚了,但它可能会帮助其他正在寻找这个问题答案的不幸的人。我在使用
hierarchyid
和 XML 时遇到了类似的性能问题:对我来说,最简单的解决方案实际上只是在
hierarchyid
上调用ToString()
选择作为 XML 列之前的 code> 值。在某些情况下,这使我的查询速度提高了十倍!这是展示问题的片段。
I realise this answer is a bit late, but it might help some other unlucky person who is searching for answers to this problem. I have had similar performance problems using
hierarchyid
with XML:It turned out for me that the simplest solution was actually just to call
ToString()
on thehierarchyid
values before selecting as an XML column. In some cases this sped up my queries by a factor of ten!Here's a snippet that exhibits the problem.