Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to loop through xml node in SQL server

Posted on 2011-03-22
4
Medium Priority
?
3,094 Views
Last Modified: 2012-08-13
I have a table (A), which has one field (outerxml), data type is xml. I can use SELECT * FROM A WHERE OUTERXML.EXIST('\Country')=1 to query the records, very convenience.

Now I have a XML string (like <country>USA</country><postalcode>12345</postalcode>), I don't know which tag name could appear in the XML string. what's best way to get tag name (country or postalcode, not value) in a XML string?
If I can loop through xmlNode, that's would be great. BTW, I don't want to use string function to parse it.

I need to do it inside SQL server 2005 stored procedure. thanks.
0
Comment
Question by:Richard_XP
[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
4 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35192332
look at this example

DECLARE @x XML

SET @x = 

'<Employees>

  <Employee ID="101">

    <Name>Jacob</Name>

    <Department>IT</Department>

  </Employee>

  <Employee ID="354">

    <Name>Steve</Name>

    <Department>IT</Department>

  </Employee>

  <Employee ID="456">

    <Name>Bob</Name>

    <Department>IT</Department>

  </Employee>

  <Employee ID="478">

    <Name>Joe</Name>

    <Department>IT</Department>

  </Employee>

  <Employee ID="981">

    <Name>Louis</Name>

    <Department>IT</Department>

  </Employee>

</Employees>'

 

-- Total count of <Employee> Nodes

DECLARE @max INT, @i INT

SELECT 

    @max = @x.query('<e>

                        { count(/Employees/Employee) }

                    </e>').value('e[1]','int') 

 

-- Set counter variable to 1

SET @i = 1

 

-- variable to store employee name

DECLARE @EmpName VARCHAR(10)

 

-- loop starts

WHILE @i <= @max BEGIN

    -- select "Name" to the variable

    SELECT

        @EmpName = x.value('Name[1]', 'VARCHAR(20)') 

    FROM 

    @x.nodes('/Employees/Employee[position()=sql:variable("@i")]') 

    e(x)

 

    -- print the name

    PRINT @EmpName

 

    -- increment counter

    SET @i = @i + 1        

END
 

Open in new window


0
 
LVL 4

Author Comment

by:Richard_XP
ID: 35192681
sorry, doesn't help. I don't know element name (tag name) in xml string, so I can not use xml function like nodes, query, exists
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35193377
Try this out:  (the xml must be within a root tag)

DECLARE 
    @idoc INT,  
    @xml XML  
    
SELECT @xml = '<root><country>USA</country><postalcode>12345</postalcode></root>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @xml  

SELECT localname FROM OPENXML(@idoc,'/',3) 
 WHERE localname not in ('root', '#text')

EXEC sp_xml_removedocument @idoc 

/*
OUTPUT:

localname
---------
country
postalcode

*/

Open in new window


A more sophisticated version is found here:
http://beyondrelational.com/blogs/jacob/archive/2009/01/23/xquery-lab-39-retrieve-fully-qualified-path-of-attributes-and-elements.aspx
0
 
LVL 4

Author Comment

by:Richard_XP
ID: 35193934
Looks good, let me try ti tomorrow. thanks.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

618 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