I have the following XML :
<Feed>
<FeedId>10</FeedId>
<Component>
<Date>2011-10-01</Date>
<Date>2011-10-02</Date>
</Component>
</Feed>
Now if possible I would like to parse the XML into sql so it’s serialized into the following relational data:
FeedId Component_Date
10 2011-10-01
10 2011-10-02
However using the following SQL:
DECLARE @XML XML;
DECLARE @XMLNodes XML;
SET @XML = '<Feed><FeedId>10</FeedId><Component><Date>2011-10-01</Date><Date>2011-10-02</Date></Component></Feed>';
SELECT t.a.query('FeedId').value('.', 'INT') AS FeedId
,t.a.query('Component/Date').value('.', 'VARCHAR(80)') AS [Component_Date]
FROM @XML.nodes(' /Feed') AS t(a)
The closest I get is :
FeedId Component_Date
10 2011-10-012011-10-02
So the date values appear in the same row, is it possible to achieve what I want using XQuery?
You need a second call to
.nodes()since you have multiple entries inside your XML – try this:Gives me an output of: