som*_*ool 4 xml sql sql-server-2005
如果我想在根元素记录中添加一个属性,我可以从sql端执行此操作吗?
SELECT top 1 'text' as nodeA
from test as z
FOR XML AUTO, ELEMENTS, root('record')
Run Code Online (Sandbox Code Playgroud)
我想像这样生成xml:
<Root attribute="value">
<z>
<NodeA>text</NodeA>
</z>
</Root>
Run Code Online (Sandbox Code Playgroud)
使用新FOR XML PATH语法:
SELECT TOP 1
'someValue' AS '@Attribute',
'text' as 'z/NodeA'
FROM dbo.Test
WHERE....
FOR XML PATH('YourElement'), ROOT('Root')
Run Code Online (Sandbox Code Playgroud)
这会有类似的东西
<Root>
<YourElement Attribute="someValue">
<z>
<NodeA>text</NodeA>
</z>
</YourElement>
</Root>
Run Code Online (Sandbox Code Playgroud)
在这里阅读更多相关信息: