Solved

Need to generate row number using Xquery

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

21 Experts available now in Live!

Get 1:1 Help Now