Solved

SQL Recordset from XML data

Posted on 2010-08-20
2
385 Views
Last Modified: 2012-05-10
I have some xml data that i need to return in a specific format and since I have never really worked with XML I am finding myself at a brick wall.

the code samaple currently reruns the following

Model      Black Flash 1873 Watts 240 VoltsBlack Flash 1973 Watts 220 Volts
Acc.       Black Flash Floor Stand

what i need returned is

Model      Black Flash 1873 Watts 240 Volts
Model      Black Flash 1973 Watts 220 Volts
Acc.        Black Flash Floor Stand

Also the number of options/name can vary and the number of sub item under name can also vary so i need this to be very generic in nature and return in the format shown no matter how many nodes are in the xml
SET @XmlData='<options>
				<option>
					<name>Model</name>
						<option_info>
							<item> 
								<itemoption_id>5276</itemoption_id> 
								<itemoption_itemid>4566</itemoption_itemid> 
								<itemoption_weight>0</itemoption_weight> 
								<itemoption_description><![CDATA[Black Flash 1873 Watts 240 Volts]]></itemoption_description> 
								<itemoption_size>16 x 16</itemoption_size> 
								<itemoption_modifyprice>25.00</itemoption_modifyprice> 
								<itemoption_shiprule>1</itemoption_shiprule>
							</item>
							<item> 
								<itemoption_id>5276</itemoption_id> 
								<itemoption_itemid>4566</itemoption_itemid> 
								<itemoption_weight>0</itemoption_weight> 
								<itemoption_description><![CDATA[Black Flash 1973 Watts 220 Volts]]></itemoption_description> 
								<itemoption_size>16 x 16</itemoption_size> 
								<itemoption_modifyprice>25.00</itemoption_modifyprice> 
								<itemoption_shiprule>1</itemoption_shiprule>
							</item>
						</option_info>
					</option>
					<option>
						<name>Acc.</name>
							<option_info>
								<item> 
									<itemoption_id>5561</itemoption_id> 
									<itemoption_itemid>4708</itemoption_itemid> 
									<itemoption_weight>0</itemoption_weight> 
									<itemoption_description><![CDATA[Black Flash Floor Stand]]></itemoption_description> 
									<itemoption_size></itemoption_size> 
									<itemoption_modifyprice>215.00</itemoption_modifyprice> 
									<itemoption_shiprule>1</itemoption_shiprule>
								</item>
							</option_info>
						</option>
					</options>'



select 
R.i.query('name').value('.', 'varchar(30)') [Name], R.i.query('option_info/item/itemoption_description[1]').value('.', 'varchar(100)') [itemoption_description]
FROM @XmlData.nodes('/options/option') R(i)

Open in new window

0
Comment
Question by:ChevelleNV
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33489812

declare @xmldata xml

SET @XmlData='<options>

				<option>

					<name>Model</name>

						<option_info>

							<item> 

								<itemoption_id>5276</itemoption_id> 

								<itemoption_itemid>4566</itemoption_itemid> 

								<itemoption_weight>0</itemoption_weight> 

								<itemoption_description><![CDATA[Black Flash 1873 Watts 240 Volts]]></itemoption_description> 

								<itemoption_size>16 x 16</itemoption_size> 

								<itemoption_modifyprice>25.00</itemoption_modifyprice> 

								<itemoption_shiprule>1</itemoption_shiprule>

							</item>

							<item> 

								<itemoption_id>5276</itemoption_id> 

								<itemoption_itemid>4566</itemoption_itemid> 

								<itemoption_weight>0</itemoption_weight> 

								<itemoption_description><![CDATA[Black Flash 1973 Watts 220 Volts]]></itemoption_description> 

								<itemoption_size>16 x 16</itemoption_size> 

								<itemoption_modifyprice>25.00</itemoption_modifyprice> 

								<itemoption_shiprule>1</itemoption_shiprule>

							</item>

						</option_info>

					</option>

					<option>

						<name>Acc.</name>

							<option_info>

								<item> 

									<itemoption_id>5561</itemoption_id> 

									<itemoption_itemid>4708</itemoption_itemid> 

									<itemoption_weight>0</itemoption_weight> 

									<itemoption_description><![CDATA[Black Flash Floor Stand]]></itemoption_description> 

									<itemoption_size></itemoption_size> 

									<itemoption_modifyprice>215.00</itemoption_modifyprice> 

									<itemoption_shiprule>1</itemoption_shiprule>

								</item>

							</option_info>

						</option>

					</options>'

select 

R.i.query('../../../name').value('.', 'varchar(30)') [Name],

R.i.value('.', 'varchar(100)') [itemoption_description]

FROM @XmlData.nodes('/options/option/option_info/item/itemoption_description') R(i)

Open in new window

0
 

Author Closing Comment

by:ChevelleNV
ID: 33489949
Thanks, i tried doing something similar. but it didn't work and i knew it was going to be a simple answer thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 help 7 98
format nvarchar field as mm/dd/yyyy 4 62
configure service broker on all databases 2 78
How to query LOCK_ESCALATION 4 40
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now