• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

Import data from XML variable into SQL Server 2008 r2 table

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
-------------------------------

Thanks
0
kishan66
Asked:
kishan66
  • 3
  • 2
  • 2
1 Solution
 
Shaun KlineLead Software EngineerCommented:
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

0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
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

0
 
kishan66Author Commented:
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".
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Shaun KlineLead Software EngineerCommented:
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.
0
 
kishan66Author Commented:
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.
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
I don't really understand your comment.

If you use this code:

truncate table dbo.tblUserMst
go
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?
0
 
Shaun KlineLead Software EngineerCommented:
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.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now