Solved

Xml To table

Posted on 2011-09-07
2
210 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
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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
Comment Utility
Awesome
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article I will describe the Detach & Attach 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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now