Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Querying XML using OPENXML WITH - mixed attribute-centric and element-centric

Posted on 2011-09-29
5
770 Views
Last Modified: 2012-05-12
Hello,

I have an XML structure that is mixed - both attribute-centric and element-centric.  For example:

<stats>
<year>2011</year>
<stat num="33" type="days_attended"/>
<stat num="12" type="referrals"/>
</stats>

I'm using element-centric to grab the data.  For example:

SELECT year,days_attended,referrals
FROM OPENXML (@mydata, //xml-tree/stats',2)
WITH
(
year int 'year',
days_attended int ???,
referrals int ???
)

How would I grab the value of "num" based upon the type = "days_attended" for example.
I know I can grab num using something like:

num int 'stat/@num'

...But how would I know which num it is.  I want to be able to grab that num value and call the column by the type value (days_attended for example).

Thanks in advance!
0
Comment
Question by:adrian78
  • 3
  • 2
5 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36890122
I will have to check on the OPENXML version to confirm it is the similar, but here is how I would do this using the XML data type.
DECLARE @mydata XML
SET @mydata = '<stats>
<year>2011</year>
<stat num="33" type="days_attended"/>
<stat num="12" type="referrals"/>
</stats>'
;

SELECT x.s.value('year[1]', 'INT') [year]
     , x.s.value('stat[@type="days_attended"][1]/@num', 'INT') [days_attended]
     , x.s.value('stat[@type="referrals"][1]/@num', 'INT') [referrals]
FROM @mydata.nodes('//stats') x(s)
;

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36890145
Okay, confirmed. Here is the OPENXML version.
-- http://technet.microsoft.com/en-us/library/ms186918.aspx
DECLARE @mydata INT
DECLARE @xml VARCHAR(8000)
SET @xml = '<stats>
<year>2011</year>
<stat num="33" type="days_attended"/>
<stat num="12" type="referrals"/>
</stats>'
;

EXEC sp_xml_preparedocument @mydata OUTPUT, @xml

SELECT [year], days_attended, referrals
FROM OPENXML(@mydata, '//stats', 2)
WITH
(
year int 'year',
days_attended int 'stat[@type="days_attended"]/@num',
referrals int 'stat[@type="referrals"]/@num'
)

EXEC sp_xml_removedocument @mydata

Open in new window

0
 

Author Comment

by:adrian78
ID: 36893846
That worked!  You are a superstar! Thank you very much for your assistance.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36894284
You are most welcome!
Best regards and happy coding,

Kevin
0
 

Author Comment

by:adrian78
ID: 36898565
Hey Kevin,

I ran into another issue!  I've posted this under a new question - your help would be greatly appreciated!

Thanks,
Adrian
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query to calculate avaerage 21 52
Currency in SQL? 2 30
MS SQL Server time between records 14 46
shrink table after huge delete 2 14
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question