Solved

Need SQL query help - SQL Server

Posted on 2010-08-13
3
264 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:
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query optimization 6 29
Display SQL 2008 last modified/update Database 11 72
Adding SQL Server Browser in after install is complete 8 50
SQL query 45 27
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

742 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