Import data from XML variable into SQL Server 2008 r2 table

Posted on 2014-10-03
Last Modified: 2015-03-09
hI ,

im looking for Sql statement to import data from XML variable and dump into a table.
I only need data fro within the <Pption></Option> tags below.
CREATE TABLE [dbo].[tblUserMst](
      [UserID] [int] IDENTITY(1,1) NOT NULL,
      [UserName] [varchar](500) NULL)
declare @input xml = '<OPTION value=bk12_info.shtml>ALEX</OPTION>
 <OPTION value=bk11_info.shtml>JOHN</OPTION>'
-------------OutPut -------
UserID     UserName
1               Alex
2               John

Question by:kishan66
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
  • 3
  • 2
  • 2
LVL 26

Expert Comment

by:Shaun Kline
ID: 40359589
Something like this?
declare @input xml 
SET @input = '<OPTION value="bk12_info.shtml">ALEX</OPTION><OPTION value="bk11_info.shtml">JOHN</OPTION>'

SELECT ROW_NUMBER() OVER(ORDER BY x.value('.', 'varchar(50)')) AS ID, x.value('.', 'varchar(50)') AS Name
FROM @input.nodes('//OPTION') AS data(x)

Open in new window

LVL 24

Expert Comment

by:Phillip Burton
ID: 40359636
insert into dbo.tblUserMst(UserName)
select Rows.n.value('.','nvarchar(100)')
from @input.nodes('/OPTION') Rows(n)

select * from dbo.tblUserMst

Open in new window


Author Comment

ID: 40360512
Guys (Shaun, Phillip)

Both of your code works only if i have one Option node  like...
<Option  value="bk12_info.shtml>one</Option>.  [Works]

if there are multiple like
<Option  value="bk12_info.shtml>one</Option>
<Option  value="bk13_info.shtml>two</Option>

It throws error "XML parsing: line 1, character 50, A string literal was expected".
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 26

Expert Comment

by:Shaun Kline
ID: 40360722
The value property needs to be enclosed in quotation marks, as it is in my example. The example you show only has the quotation mark on one side of the value property.

Author Comment

ID: 40362339
Shaun, Thanks for the the reply.
But in reality the xml i receive doesn't have value enclosed in quotation marks.  More over i donot need that value either.
LVL 24

Accepted Solution

Phillip Burton earned 500 total points
ID: 40363141
I don't really understand your comment.

If you use this code:

truncate table dbo.tblUserMst
declare @input xml = '<Option value="bk12_info.shtml">one</Option><Option value="bk13_info.shtml">two</Option>'
insert into dbo.tblUserMst(UserName)
select Rows.n.value('.','nvarchar(100)')
from @input.nodes('/Option') Rows(n)
select * from dbo.tblUserMst

Open in new window

You will get this answer

UserID   UserName
1             one
2             two

Do you not get the same result?
LVL 26

Expert Comment

by:Shaun Kline
ID: 40363542
If your data is not coming with the properties of your XML node enclosed in quotation marks, then the XML is not well-formed and attempts to use it as XML will fail, unless you transform it into well-formed XML.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
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…

717 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