我想创建一个包含属性和值的xml节点。
示例:
<mynode attr1="hello">world</mynode>
我理解
SELECT 'hello' as '@attr1'
创建至少mynode,但是如何附加“world”呢?
我试过
SELECT 'world' as 'mynode/[@attr1=hello]'
但它无效
答案 0 :(得分:11)
这是一个基本的例子:
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
答案 1 :(得分:5)
这有助于你
SELECT TOP 1
'someValue' AS '@Attribute',
'text'
FOR XML PATH('YourElement'), ROOT('Root')
答案 2 :(得分:2)
select 'hello' as [@attr1]
, 'world'
for xml path('mynode')