Solved

Need SQL query help - SQL Server

Posted on 2010-08-13
3
260 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
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
faiga16 earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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