3

我目前正在使用 T-SQL 将一些数据转换为 XML。我需要在自己的行上列出属性的值。出于某种原因,T-SQL 到 XML 一直在同一行将值连接在一起。

示例代码:

SELECT
    'Fruits' AS [Attribute/@name],
    'Apple' AS [Attribute/Value],
    'Orange' AS [Attribute/Value],
    'Grape' AS [Attribute/Value]
FOR XML PATH (''), ROOT('CustomProduce'), TYPE

样本结果:

<CustomProduce>
  <Attribute name="Fruits">
    <Value>AppleOrangeGrape</Value>
  </Attribute>
</CustomProduce>

期望的结果:

<CustomProduce>
  <Attribute name="Fruits">
    <Value>Apple</Value>
    <Value>Orange</Value>
    <Value>Grape</Value>
  </Attribute>
</CustomProduce>

任何帮助将不胜感激,非常感谢!

4

1 回答 1

1

一种(相当荒谬的)方法,以防没有人知道更好的解决方案:

SELECT
    'Fruits' AS [Attribute/@name],
    '' AS [Attribute],
    'Apple' AS [Attribute/Value],
    '' AS [Attribute],
    'Orange' AS [Attribute/Value],
    '' AS [Attribute],
    'Grape' AS [Attribute/Value]
FOR XML PATH (''), ROOT('CustomProduce'), TYPE
于 2013-05-14T14:56:37.723 回答