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
Solved

how to loop through xml node in SQL server

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

829 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