Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Recordset from XML data

Posted on 2010-08-20
2
Medium Priority
?
392 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
[X]
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
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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