使用SQL生成XML

时间:2015-10-06 15:59:17

标签: sql xml sql-server-2008

我尝试使用SQL以以下格式生成XML:

<ImportSession>
  <Batches>
    <Batch>
      <BatchFields>
        <BatchField Name="Field1" Value="1" />
        <BatchField Name="Field2" Value="2" />
        <BatchField Name="Field3" Value="3" />
      </BatchFields>
    <Batch>
  <Batches>
</ImportSession>

我正在使用SQL Server 2008.我写了这个查询:

SELECT
    (SELECT
         (SELECT 
              'Col' AS [@Name],
              FiscalYear AS [@Value]
          FROM [ICEM].[dbo].[ExportedBill]
          WHERE ExportedBillID = 1
          FOR XML PATH ('BatchField'), TYPE)
     FROM [ICEM].[dbo].[ExportedBill]
     WHERE ExportedBillID = 1
     FOR XML PATH ('BatchFields'), ROOT ('Batch'), TYPE)
FROM
    [ICEM].[dbo].[ExportedBill]
WHERE
    ExportedBillID = 1
FOR XML PATH ('Batches'), ROOT ('ImportSession')

这导致:

<ImportSession>
  <Batches>
    <Batch>
      <BatchFields>
        <BatchField Name="Col" Value="2015" />
      </BatchFields>
    </Batch>
  </Batches>
</ImportSession>

我需要的是每列都应该在BatchField中有一个条目。此外,我需要列名称显示在名称中。所以我应该得到:

<BatchField Name="FiscalYear" Value="2015" />
<BatchField Name="MeterNumber" Value="123456" />
<BatchField Name="Name" Value="John Smith" />
<BatchField Name="Utility" Value="Electricity" />

那么有人能告诉我如何修改我的查询以获得我需要的东西吗?

编辑:

我明白了。我需要第二个嵌套的Select。每列我需要一个。如果他们继续选择使用与之前的选择相同的标签,那么信息将在同一父标签下连接

SELECT
    (SELECT
         (SELECT 
              'FiscalYear' AS [@Name],
              FiscalYear AS [@Value]
          FROM [ICEM].[dbo].[ExportedBill]
          WHERE ExportedBillID = 1
          FOR XML PATH ('BatchField'), TYPE),
          (SELECT 'FiscalPeriod' AS [@Name],
            FiscalPeriod AS [@Value]
          FROM [PEEL_ICEM].[dbo].[ExportedBill]
          WHERE ExportedBillID = 1
          FOR XML PATH ('BatchField'), TYPE)
     FROM [ICEM].[dbo].[ExportedBill]
     WHERE ExportedBillID = 1
     FOR XML PATH ('BatchFields'), ROOT ('Batch'), TYPE)
FROM
    [ICEM].[dbo].[ExportedBill]
WHERE
    ExportedBillID = 1
FOR XML PATH ('Batches'), ROOT ('ImportSession')
但是,事情是,这个表中将有大约70列。我现在生病了,但如果有人知道更好的方法,请告诉我。干杯

1 个答案:

答案 0 :(得分:1)

您可以通过添加空白列分隔符来创建单独的子元素。 e.g。

DECLARE @T TABLE 
(   FiscalYear INT, 
    MeterNumber INT, 
    Name VARCHAR(255), 
    Utility VARCHAR(255)
);
INSERT @T VALUES (2015, 123456, 'John Smith', 'Electricity');

SELECT  [BatchField/@Name] = 'FiscalYear',
        [BatchField/@Value] = FiscalYear, 
        '',
        [BatchField/@Name] = 'MeterNumber',
        [BatchField/@Value] = MeterNumber,
        '',
        [BatchField/@Name] = 'Name',
        [BatchField/@Value] = Name,
        '',
        [BatchField/@Name] = 'Utility',
        [BatchField/@Value] = Utility
FROM    @T
FOR XML PATH('BatchFields'), ROOT('Batch');

给出了:

<Batch>
  <BatchFields>
    <BatchField Name="FiscalYear" Value="2015" />
    <BatchField Name="MeterNumber" Value="123456" />
    <BatchField Name="Name" Value="John Smith" />
    <BatchField Name="Utility" Value="Electricity" />
  </BatchFields>
</Batch>