Solved

Need to generate row number using Xquery

Posted on 2010-08-15
2
1,128 Views
Last Modified: 2013-11-26
Hi,

I would like to generate a unique row number ( starting from 1 ) to the
result set generated from the query below.   I know that I could insert the
results into a table with an identity column, but I need to have
a unique row number produced without doing any table inserts.


BEGIN

DECLARE @TheXML XML

SELECT

      SUBSTRING(doc.col.value('first_name[1]', 'varchar(30)'),1,30) first_name,
      SUBSTRING(doc.col.value('surname[1]', 'varchar(35)') ,1,35) surname,
      SUBSTRING(doc.col.value('title[1]', 'varchar(4)') ,1,4) title,
      SUBSTRING(doc.col.value('gender[1]', 'varchar(6)') ,1,6) gender


FROM @thexml.nodes('/jobs/applicant') doc(col)

END

Thanks
0
Comment
Question by:cintra
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 33439982
BEGIN
DECLARE @TheXML XML
set @TheXML = '<jobs>
<applicant><firstname>jim</firstname><surname>john</surname><title>mr</title><gender>m</gender></applicant>
<applicant><firstname>tim</firstname><surname>jack</surname><title>mr</title><gender>m</gender></applicant>
<applicant><firstname>tom</firstname><surname>jill</surname><title>mrs</title><gender>f</gender></applicant>
</jobs>'
SELECT
      Row_Number() over (order by doc.col.value('@position','int')),
      SUBSTRING(doc.col.value('first_name[1]', 'varchar(30)'),1,30) first_name,
      SUBSTRING(doc.col.value('surname[1]', 'varchar(35)') ,1,35) surname,
      SUBSTRING(doc.col.value('title[1]', 'varchar(4)') ,1,4) title,
      SUBSTRING(doc.col.value('gender[1]', 'varchar(6)') ,1,6) gender
FROM @thexml.nodes('/jobs/applicant') doc(col)
END
0
 

Author Closing Comment

by:cintra
ID: 33443381
Thnak you. That's exactly what I was looling for?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

11 Experts available now in Live!

Get 1:1 Help Now