如何在 sql server 2005 中为 xml 节点添加属性 [英] How to add attributes to xml nodes in sql server 2005

查看:46
本文介绍了如何在 sql server 2005 中为 xml 节点添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想给根元素记录添加一个属性,我可以从 sql 端做这个吗?

If i wanted to add an attribute to the root element record, can i do this from the sql side?

SELECT top 1 'text' as nodeA
                from test as z
FOR XML AUTO, ELEMENTS, root('record')

我想像这样生成xml:

i would like to produce the xml like this:

 <Root attribute="value">
     <z>
         <NodeA>text</NodeA>
     </z>
 </Root>

推荐答案

使用新的 FOR XML PATH 语法:

SELECT TOP 1 
   'someValue' AS '@Attribute',
   'text' as 'z/NodeA'
FROM dbo.Test
WHERE....
FOR XML PATH('YourElement'), ROOT('Root')

这将给出类似

<Root>
   <YourElement Attribute="someValue">
      <z>
         <NodeA>text</NodeA>
      </z>
   </YourElement>
</Root>

在此处阅读更多相关信息:

Read more about it here:

这篇关于如何在 sql server 2005 中为 xml 节点添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆