Solved

how to loop through xml node in SQL server

Posted on 2011-03-22
4
2,659 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
  • 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now