?
Solved

how to loop through xml node in SQL server

Posted on 2011-03-22
4
Medium Priority
?
2,990 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

800 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