I have an XML variable that contains the following XML:
<Fruits>
<Fruit>
<Type>Apple</Type>
<FruitID>1</FruitID>
<MoreInfo>
<Info Name="GreenApples" Value="3900" />
</MoreInfo>
</Fruit>
<Fruit>
<Type>Orange</Type>
<FruitID>2</FruitID>
<MoreInfo>
<Info Name="Oranges" Value="1100" />
</MoreInfo>
</Fruit>
</Fruits>
I am trying to create a temp table that looks like that:
CREATE TABLE #XmlTable (
email nvarchar(100),
xmlNode XML
)
The output that I am aiming for is:
email | xmlNode
[email protected] | <Fruit><Type>Apple</Type><FruitID>1</FruitID><MoreInfo><Info Name="GreenApples" Value="3900" /></MoreInfo></Fruit>
[email protected] | <Fruit><Type>Orange</Type><FruitID>2</FruitID><MoreInfo><Info Name="Oranges" Value="1100" /></MoreInfo></Fruit>
The problem is that I don't know how to convert it to XML when I split it and insert it in the table.
I have the following SQL that is doing the job, but how do I convert it to XML?
INSERT INTO #XmlTable
SELECT EOR.email,
xmlNode.Col.value('.', 'nvarchar(max)') AS XML
FROM @outputXML.nodes('/Fruits') xmlNode(Col)
CROSS APPLY @outputXML.nodes('/Fruits/Fruit/FruitID') xmlValue(i)
INNER JOIN #EmailsOfReceivers EOR
ON EOR.ID= xmlValue.i.value('.','nvarchar(max)')
It works fine, but of course the xmlNode column doesn't have the XML tags.