0

I'm trying to use SQL to generate XML in the format:

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

I'm using SQL Server 2008. I wrote this query:

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')

And this results in:

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

What I need though is every column should have an entry in BatchField. Also I need the column name to show up in the name. So I should get:

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

So can anyone tell me how I modify my query to get what I need?

EDIT:

I figured it out. I needed a second nested Select. I need one for each column. If they proceeding selects use the same tags as a previous Select then the information is concatanated under the same parent tag

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')

Thing is though, there will be around 70 columns in this table. Ill brute force it for now, but if anyone knows of a better way to do this please let me know. Cheers

1 Answer 1

1

You can create separate child elements by adding a blank column separator. 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');

Which gives:

<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>    
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers man. That helps a lot. I wasnt looking forward to the state my query was gonna be in if I had done it my way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.