Solved

SQL Recordset from XML data

Posted on 2010-08-20
2
383 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

707 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

12 Experts available now in Live!

Get 1:1 Help Now