如何使用 XQuery 将 xml 插入另一个 xml 中的节点?
我有两个 xml 变量,比如 SQL Server 2005 中的存储过程中的 @res、@student。
@res contains
<Subject>English</Subject>
<Marks>67</Marks>
<Subject>Science</Subject>
<Marks>75</Marks>
@student contains:
<Student>
<Name>XYZ</Name>
<Roll>15</Roll>
<Result />
<Attendance>50</Attendance>
</Student>
我需要将 @res 的 xml 插入到 @student 中的节点 Result 中使用 XQuery 的变量。
如何实施?
请帮忙。
I have two xml variable say @res, @student in a stored proc in SQL server 2005.
@res contains
<Subject>English</Subject>
<Marks>67</Marks>
<Subject>Science</Subject>
<Marks>75</Marks>
@student contains:
<Student>
<Name>XYZ</Name>
<Roll>15</Roll>
<Result />
<Attendance>50</Attendance>
</Student>
I need to insert the xml of @res into the node Result in @student variable using XQuery.
How to implement that?
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SQL Server 2008 中,这非常简单:
这给了我输出:
不幸的是,调用
.modify()
并在插入语句中使用sql:variable
的能力是仅在 SQL Server 2008 中引入 - 在 SQL Server 2005 中不起作用。除了求助于丑陋的字符串解析和替换之外,我不知道如何在 SQL Server 2005 中执行此操作:
Marc
In SQL Server 2008, it's pretty easy:
That gives me the output:
Unfortunately, the ability to call
.modify()
and use asql:variable
in the insert statement was introduced with SQL Server 2008 only - doesn't work in SQL Server 2005.I don't see how you could do this in SQL Server 2005, other than resorting back to ugly string parsing and replacement:
Marc
这将在 SQL 2005 中工作,并且主要是一个 xquery 解决方案:
如果需要的话,您可以在此时将 @student 变量设置为 @final。节点的“test”名称正是我选择使用的名称。您可以使用任何名称只要它尚未出现在您的 XML 中即可。
基本上只需将两个 XML 字符串放在一起,以便它们同时可用于 xquery。
This will work in SQL 2005 and is mostly an xquery solution:
You can set your @student variable to @final at that point if you need to do that. The name of "test" for the node was just what I chose to use. You can use any name as long as it will not already appear in your XML.
You basically just throw the two XML strings together so that they are both available to xquery at once.
您也可以尝试返回到关系数据而不是返回到 xml;类似于:
Returns:
不完全是您的示例,但很接近。
You may also try to go back to relational data and than back to xml; something like:
Returns:
Not exactly your example, but close.