Solved

Need to generate row number using Xquery

Posted on 2010-08-15
2
1,135 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
abstract class C# 1 31
SSRS Deployment problem 5 67
C# class library debugging - Breakpoint will not hit. 8 21
xml files 7 29
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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