Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Recordset from XML data

Posted on 2010-08-20
2
Medium Priority
?
394 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

927 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