I have the following table structure
ZoneID int
ZoneName varchar(50)
I need to get this into an XML structure as follows to import into another system
<Batch>
<Record>
<Insert>
<Field>
<FieldName>ZoneID</FieldName>
<FieldValue>1</FieldValue>
</Field>
<Field>
<FieldName>ZoneName</FieldName>
<FieldValue>Interior</FieldValue>
</Field>
</Insert>
</Record>
<Record>
<Insert>
<Field>
<FieldName>ZoneID</FieldName>
<FieldValue>2</FieldValue>
</Field>
<Field>
<FieldName>ZoneName</FieldName>
<FieldValue>Exterior</FieldValue>
</Field>
</Insert>
</Record>
</Batch>
So far I have this:
SELECT
(
(SELECT
'ZoneID' as 'FieldName',
[ZoneID] as 'FieldValue'
FROM [dbo].[Condition_t_Zones]
WHERE ZoneID = [Condition_t_Zones].ZoneID
FOR XML PATH('field'), TYPE)
) as 'Insert'
FROM [Condition_t_Zones]
FOR XML PATH('record'), ROOT('batch')
But am unable to work out how to select the other field ‘ZoneName’ using another subquery.
Any help greatly appreciated.
Managed to get it working, think from my previous tests I had commas and Parentheses in the wrong places.
Solution I arrived at as follows: