我的查询如下(样本)。
Select 'Somthing' as Title,
'Some notes' as Notes,
(Select Path1
From (Select 'One' Path1
union
Select 'Two' Path1
union
Select 'Three' Path1) T
FOR XML PATH('Image'),ROOT('Images'), ELEMENTS, TYPE),
'Other value' as Value
FOR XML PATH('ItemRow'),TYPE,ELEMENTS
xml以下的输出
<ItemRow>
<Title>Somthing</Title>
<Notes>Some notes</Notes>
<Images>
<Image>
<Path1>One</Path1>
</Image>
<Image>
<Path1>Two</Path1>
</Image>
<Image>
<Path1>Three</Path1>
</Image>
</Images>
<Value>Other value</Value>
</ItemRow>
我正在尝试将Notes和Images放入父节点,因此它应该如下所示
<ItemRow>
<Title>Somthing</Title>
<SomeParentNode>
<Notes>Some notes</Notes>
<Images>
<Image>
<Path1>One</Path1>
</Image>
<Image>
<Path1>Two</Path1>
</Image>
<Image>
<Path1>Three</Path1>
</Image>
</Images>
</SomeParentNode>
<Value>Other value</Value>
</ItemRow>
这可能吗?
答案 0 :(得分:2)
只需像这样添加SomeParentNode
:
Select 'Somthing' as Title,
'Some notes' as 'SomeParentNode/Notes', -- here
(Select Path1
From (Select 'One' Path1
union
Select 'Two' Path1
union
Select 'Three' Path1) T
FOR XML PATH('Image'),ROOT('Images'), ELEMENTS, TYPE) AS 'SomeParentNode', -- and here
'Other value' as [Value]
FOR XML PATH('ItemRow'),TYPE,ELEMENTS
输出:
<ItemRow>
<Title>Somthing</Title>
<SomeParentNode>
<Notes>Some notes</Notes>
<Images>
<Image>
<Path1>One</Path1>
</Image>
<Image>
<Path1>Two</Path1>
</Image>
<Image>
<Path1>Three</Path1>
</Image>
</Images>
</SomeParentNode>
<Value>Other value</Value>
</ItemRow>
答案 1 :(得分:1)
SELECT
'Something' AS Title,
(
SELECT
'Some Notes' AS Notes,
(
SELECT
Path1
FROM
(VALUES('One'),('Two'),('Three')) AS T(Path1)
FOR
XML PATH('Image'), TYPE
)
FOR
XML PATH('Images'), TYPE
),
'Other value' as Value
FOR
XML PATH('ItemRow')
SELECT
'Something' AS Title,
'Some Notes' AS [Images/Notes],
(
SELECT
Path1
FROM
(VALUES('One'),('Two'),('Three')) AS T(Path1)
FOR
XML PATH('Image'), TYPE
) AS [Images],
'Other value' as Value
FOR
XML PATH('ItemRow')
两者都导致:
<ItemRow>
<Title>Something</Title>
<Images>
<Notes>Some Notes</Notes>
<Image>
<Path1>One</Path1>
</Image>
<Image>
<Path1>Two</Path1>
</Image>
<Image>
<Path1>Three</Path1>
</Image>
</Images>
<Value>Other value</Value>
</ItemRow>