?
Solved

Need to generate row number using Xquery

Posted on 2010-08-15
2
Medium Priority
?
1,161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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