Create Table #tmp(ID int IDentity(1,1), XMLData XML)
Insert Into #tmp(XMLData)
Values('<SampleXML>
<Fruit>
<Fruits>Apple</Fruits>
<Fruits>Pineapple</Fruits>
</Fruit>
<Fruit>
<Fruits>Grapes</Fruits>
<Fruits>Melon</Fruits>
</Fruit>
</SampleXML>')
SELECT
ID,
A.x.query('data(.)') as name,
Row_Number() over(order by A.x) as number
FROM #tmp
CROSS APPLY XMLData.nodes('SampleXML/Fruit/Fruits') AS A(x)
This results into the following:
ID name number
1 Apple 1
1 Pineapple 2
1 Grapes 3
1 Melon 4
but I do want it to look like this:
ID name number
1 Apple 1
1 Pineapple 1
1 Grapes 2
1 Melon 2
I want to know in which "Fruit" element the "Fruits" were found.