Sql - FOR XML 路径查询问题
Sql - FOR XML Path query issue
我的查询如下(样本)。
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>
我正在尝试将注释和图像放入父节点,因此它应该如下所示
<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>
这可能吗?
- 漫长的道路,使用子查询将元素放置在正确的路径中:
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 将元素放在适当的位置
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>
只需像这样添加 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>
我的查询如下(样本)。
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>
我正在尝试将注释和图像放入父节点,因此它应该如下所示
<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>
这可能吗?
- 漫长的道路,使用子查询将元素放置在正确的路径中:
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 将元素放在适当的位置
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>
只需像这样添加 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>