2015-10-06 64 views
0

我试图使用SQL生成的XML格式:使用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 
       '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') 

事情是,虽然下concatanated,会出现在该表中的70列。虐待现在,但如果有人知道更好的方式来做到这一点,请让我知道。欢呼声

回答

1

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

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>  
+0

干杯的人。这有很大帮助。如果我按照自己的方式完成了这个任务,我不会期待这个状态,因为我的查询会处在这个状态。 – discodowney