Solved

Xml To table

Posted on 2011-09-07
2
220 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 500 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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