2

我有一个名为 StoreFolder 的表,其中包含一个名为 FolderXml 的 XML 字段。每行包含一个 XML 值,如下所示:

<Folder FolderName="test" StoreName="TestStore" />

我需要获取以下 XML 格式的文件夹:

<Folders>
  <Folder FolderName="test1" StoreName="TestStore1" />
  <Folder FolderName="test2" StoreName="TestStore1" />
  <Folder FolderName="test3" StoreName="TestStore1" />
</Folders>

我尝试使用以下 SQL 语句:

SELECT   FolderXml
FROM     StoreFolder
FOR XML PATH(''), ROOT('Folders')

但它输出以下 XML:

<Folders>
  <FolderXml>
    <Folder FolderName="test1" StoreName="TestStore1" />
  </FolderXml>
  <FolderXml>
    <Folder FolderName="test2" StoreName="TestStore1" />
  </FolderXml>
  <FolderXml>
    <Folder FolderName="test3" StoreName="TestStore1" />
  </FolderXml>
</Folders>

有人可以告诉我如何获取以下格式的 XML(使用 T-SQL):

<Folders>
  <Folder FolderName="test1" StoreName="TestStore1" />
  <Folder FolderName="test2" StoreName="TestStore1" />
  <Folder FolderName="test3" StoreName="TestStore1" />
</Folders>

非常感谢

4

3 回答 3

3

您只是缺少转换/转换操作:

select Convert(Xml, FolderXml)
  from StoreFolder
for xml path(''), root('Folders')

您的FolderXml字段被视为 varchar/nvarchar 字段,然后添加到 Xml 节点中。预先将其转换为 Xml 以确保它不会被包装在父标记中。

于 2012-12-07T14:17:33.567 回答
0
select FolderName as "@foldername",StoreName as "@StoreName"
FROM StoreFolder
FOR XML PATH('Folder'),ROOT('Folder')
于 2012-12-07T13:47:45.380 回答
0
DECLARE @T TABLE(
    FolderXml XML
)

INSERT INTO @T VALUES
('<Folder FolderName="test1" StoreName="TestStore1" />'),
('<Folder FolderName="test2" StoreName="TestStore1" />'),
('<Folder FolderName="test3" StoreName="TestStore1" />')


SELECT (SELECT T.FolderXml)
FROM @T AS T
FOR XML PATH (''), ROOT('Folders')
于 2012-12-07T14:19:57.640 回答