Solved

SQL XML Select:  Need to get the index of the node

Posted on 2013-11-21
2
404 Views
Last Modified: 2013-11-22
OK

I am using this as a split function and need to insert the split into a table.

I don't want to put the results into a temp table if I don't have to.

DECLARE @value varchar(50)
SET @value = '9|5|22|298'
      
SET @xml = N'<root><r>' + replace(@value,'|','</r><r>') + '</r></root>'
SELECT
    --I NEED THE INDEX OF THE NODE HERE
      r.value('.','varchar(50)')
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

--I Want to do something like this

INSERT INTO TABLE (order, valueID)
SELECT
    --I NEED THE INDEX OF THE NODE HERE
      r.value('.','varchar(50)')
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

Thanks for the help
0
Comment
Question by:skillilea
2 Comments
 
LVL 12

Accepted Solution

by:
Saurabh Bhadauria earned 500 total points
ID: 39668164
Do it like this..
DECLARE @value varchar(50)
DECLARE @xml XML 
SET @value = '9|5|22|298'

      
SET @xml = N'<root><r>' + replace(@value,'|','</r><r>') + '</r></root>'
SELECT 
    ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS seq,
      r.value('.','varchar(50)') 
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

--I Want to do something like this

INSERT INTO TABLE (order, valueID)
SELECT 
    ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS seq,
      r.value('.','varchar(50)') 
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)'))

Open in new window



Thanks,
Saurabh
0
 

Author Closing Comment

by:skillilea
ID: 39668386
Awesome!

Just what I needed...tnx tons
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

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.
This article discusses how to create an extensible mechanism for linked drop downs.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

862 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

23 Experts available now in Live!

Get 1:1 Help Now