?
Solved

Need SQL query help - SQL Server

Posted on 2010-08-13
3
Medium Priority
?
267 Views
Last Modified: 2012-05-10

-- Declare XML string variable


DECLARE @XMLString varchar(5000)


-- Set value to the XML string variable


SET @XMLString ='
<ROOT>
  <row au_id="172-32-1176"
       au_lname="White"
       au_fname="Johnson"
       phone="408 496-7223"
       address="10932 Bigge Rd."
       city="Menlo Park"
       state="CA"
       zip="94025"
       contract="1">
  </row>
  <row au_id="213-46-8915"
       au_lname="Green"
       au_fname="Marjorie"
       phone="415 986-7020"
       address="309 63rd St. #411"
       city="Oakland"
       state="CA"
       zip="94618"
       contract="1">
  </row>
</ROOT>'


-- NODES


SELECT       c.value('@au_lname', 'varchar(25)'),

      c.value('@au_fname', 'varchar(25)')

FROM @XMLString.nodes('/ROOT/row') T(c)

This gives an error. Can you please provide me necessary SQL statement by fixing this ?

Thanks
0
Comment
Question by:milani_lucie
[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
3 Comments
 
LVL 15

Accepted Solution

by:
Faiga Diegel earned 2000 total points
ID: 33434534
Use something like this:

drop table #myXML
go
create table #myXML (id int identity(1,1) primary key,
        xm_data xml)
go
insert into #myXML (xm_data)
select
'<ROOT>
  <row au_id="172-32-1176"
       au_lname="White"
       au_fname="Johnson"
       phone="408 496-7223"
       address="10932 Bigge Rd."
       city="Menlo Park"
       state="CA"
       zip="94025"
       contract="1">
  </row>
  <row au_id="213-46-8915"
       au_lname="Green"
       au_fname="Marjorie"
       phone="415 986-7020"
       address="309 63rd St. #411"
       city="Oakland"
       state="CA"
       zip="94618"
       contract="1">
  </row>
</ROOT>'

go
SELECT       c.value('@au_lname', 'varchar(25)'),

      c.value('@au_fname', 'varchar(25)')
from #myXML cross apply xm_data.nodes('/ROOT/row') T(c)
go

The nodes should be an XML data type.

I got the same idea on this site: http://www.sqlservercentral.com/Forums/Topic732029-21-1.aspx 
0
 

Author Comment

by:milani_lucie
ID: 33434565
Sorry .... Here is the query:

DECLARE @XMLString XML

-- Set value to the XML string variable

SET @XMLString ='
<ROOT>
<row>
<au_id>172-32-1176</au_id>
<au_lname>White</au_lname>
<au_fname>Johnson</au_fname>
<phone>408 496-7223</phone>
<address>10932 Bigge Rd.</address>
<city>Menlo Park</city>
<state>CA</state>
<zip>94025</zip>
<contract>1</contract>
</row>
<row>
<au_id>213-46-8915</au_id>
<au_lname>Green</au_lname>
<au_fname>Marjorie</au_fname>
<phone>415 986-7020</phone>
<address>309 63rd St. #411</address>
<city>Oakland</city>
<state>CA</state>
<zip>94618</zip>
<contract>1</contract>
</row>
</ROOT>'

-- NODES

SELECT c.value('au_lname', 'varchar(25)') au_lname,
c.value('au_fname', 'varchar(25)') au_fname
FROM @XMLString.nodes('/ROOT/row') T(c)
Thanks
0
 

Author Comment

by:milani_lucie
ID: 33434570
This works good if the datatype is XML. Here you go:
-- Declare XML string variable


DECLARE @XMLString XML

-- Set value to the XML string variable


SET @XMLString ='
<ROOT>
  <row au_id="172-32-1176"
       au_lname="White"
       au_fname="Johnson"
       phone="408 496-7223"
       address="10932 Bigge Rd."
       city="Menlo Park"
       state="CA"
       zip="94025"
       contract="1">
  </row>
  <row au_id="213-46-8915"
       au_lname="Green"
       au_fname="Marjorie"
       phone="415 986-7020"
       address="309 63rd St. #411"
       city="Oakland"
       state="CA"
       zip="94618"
       contract="1">
  </row>
</ROOT>'


-- NODES


SELECT       c.value('@au_lname', 'varchar(25)'),

      c.value('@au_fname', 'varchar(25)')

FROM @XMLString.nodes('/ROOT/row') T(c)
 
 Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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