我想创建一个包含属性和值的xml节点.
例:
<mynode attr1="hello">world</mynode>
我明白那个
SELECT 'hello' as '@attr1'
创造至少mynode,但如何附加“世界”呢?
我试过了
SELECT 'world' as 'mynode/[@attr1=hello]'
但它不起作用
解决方法
这是一个基本的例子:
SELECT ( SELECT 'White' AS Color1,'Blue' AS Color2,'Black' AS Color3,'Light' AS 'Color4/@Special','Green' AS Color4,'Red' AS Color5 FOR XML PATH('Colors'),TYPE ),( SELECT 'Apple' AS Fruits1,'Pineapple' AS Fruits2,'Grapes' AS Fruits3,'Melon' AS Fruits4 FOR XML PATH('Fruits'),TYPE ) FOR XML PATH(''),ROOT('SampleXML')
有关更多信息,请访问此页面SQL SERVER – Simple Example of Creating XML File Using T-SQL