Solved

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

Posted on 2013-11-21
2
407 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 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