?
Solved

Xml To table

Posted on 2011-09-07
2
Medium Priority
?
226 Views
Last Modified: 2012-05-12
Hi ,
I have @xml variable, I need to convert to table, I can not grab "Answer Value" from xml
Thanks
DECLARE @tempTable TABLE
( Name NVARCHAR(50), Title NVARCHAR(50), Value NVARCHAR(50) )

INSERT INTO @tempTable SELECT Tbl.Col.value('@Name', 'NVARCHAR(50)')
          , Tbl.Col.value('@Title', 'NVARCHAR(50)')
          , Tbl.Col.value('@Value', 'NVARCHAR(50)')
            FROM @xml.nodes('//Question') Tbl(Col)

And the xml is below:
<Root>
    <Question Name="PleaseSpecify" add="canda">
    <Answer Value="" />
  </Question>
  <Question Name="aa" add="usa">
    <Answer Value="" />
  </Question>
</Root>
0
Comment
Question by:samprg
[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 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36499541
You are very close. Just fix the XPath.
DECLARE @tempTable TABLE(Name NVARCHAR(50), Title NVARCHAR(50), Value NVARCHAR(50));
DECLARE @xml XML;
SET @xml = '
<Root>
  <Question Name="PleaseSpecify" add="canda">
    <Answer Value="Test" />
  </Question>
  <Question Name="aa" add="usa">
    <Answer Value="Example" />
  </Question>
</Root>
';

INSERT INTO @tempTable(Name, Title, Value)
SELECT Tbl.Col.value('@Name', 'NVARCHAR(50)')
     , Tbl.Col.value('@Title', 'NVARCHAR(50)')
     , Tbl.Col.value('Answer[1]/@Value', 'NVARCHAR(50)')
FROM @xml.nodes('//Question') Tbl(Col)
;

SELECT Name, Title, Value
FROM @tempTable
;

Open in new window

0
 

Author Closing Comment

by:samprg
ID: 36499553
Awesome
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 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