SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stuck with an xml nodes query


Stuck with an xml nodes query

Author
Message
Paul Treston
Paul Treston
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 461
Hello there,

I'm currently working on extracting a load of xml elements out of a dynamic blob of xml. I have over 100 columns nailed down but 1 column from an element that I can't really reference has me stumped.

Below is an example of some xml that follows the same format that I'm trying to query.


<ROOTELEMENT>
<RESULTS Type="A" VALUE="1" />
<RESULTS Type="B" VALUE="2" />
<RESULTS Type="C" VALUE="3" />
<RESULTS Type="D" VALUE="4" />
<RESULTS Type="E" VALUE="5" />
<RESULTS Type="F" VALUE="6" />
<RESULTS Type="G" VALUE="7" />
</ROOTELEMENT>



What I need to do is pull out the value 4 from the Type D results element. The issue is that all of the elements within the rootelement are called results. The number of results can change and the position of the Type D results element can also change.

I'm using the .nodes .query method to pull out all the other values that I have required so far in one sql select statement, so I would like to stick with this method if at all possible.

Any one have any suggestions ?
Paul Treston
Paul Treston
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 461
Nevermind, I've figured it out.
Alan.B
Alan.B
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5215 Visits: 7731
Paul Treston (11/11/2013)
Hello there,

I'm currently working on extracting a load of xml elements out of a dynamic blob of xml. I have over 100 columns nailed down but 1 column from an element that I can't really reference has me stumped.

Below is an example of some xml that follows the same format that I'm trying to query.


<ROOTELEMENT>
<RESULTS Type="A" VALUE="1" />
<RESULTS Type="B" VALUE="2" />
<RESULTS Type="C" VALUE="3" />
<RESULTS Type="D" VALUE="4" />
<RESULTS Type="E" VALUE="5" />
<RESULTS Type="F" VALUE="6" />
<RESULTS Type="G" VALUE="7" />
</ROOTELEMENT>



What I need to do is pull out the value 4 from the Type D results element. The issue is that all of the elements within the rootelement are called results. The number of results can change and the position of the Type D results element can also change.

I'm using the .nodes .query method to pull out all the other values that I have required so far in one sql select statement, so I would like to stick with this method if at all possible.

Any one have any suggestions ?


It's worth noting that the nodes method may not be the best choice for what you are doing. Nodes() allows you to query multiple nodes like so:


DECLARE @x xml='
<ROOTELEMENT>
<RESULTS Type="A" VALUE="1" />
<RESULTS Type="B" VALUE="2" />
<RESULTS Type="C" VALUE="3" />
<RESULTS Type="D" VALUE="4" />
<RESULTS Type="E" VALUE="5" />
<RESULTS Type="F" VALUE="6" />
<RESULTS Type="G" VALUE="7" />
</ROOTELEMENT>'

SELECT x.c.value('@VALUE','char(1)') AS val
FROM @x.nodes('//RESULTS') x(c)



For only one value (as is the case with what you are doing) you could use nodes like this:


SELECT x.c.value('@VALUE','char(1)') AS val
FROM @x.nodes('//RESULTS') x(c)
WHERE x.c.value('@Type','char(1)')='D'



Using the nodes() method you could read much less data and return the values much faster like this:


SELECT x.c.value('@VALUE','char(1)') AS val
FROM @x.nodes('/ROOTELEMENT/RESULTS[@Type="D"]') x(c)



That said, you don't even need nodes(), you could accomplish the same using just the query() or value() like so:


SELECT @x.value('(/ROOTELEMENT/RESULTS[@Type="D"]/@VALUE)[1]','int') AS val
SELECT @x.query('data(/ROOTELEMENT/RESULTS[@Type="D"]/@VALUE)[1]') AS val



Nodes is the most powerful of these methods but is generally the slowest. I suggest testing the value() and query() methods as well to see what works and performs best for you.

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search