Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Row ID

Status
Not open for further replies.

IanWaterman

Programmer
Joined
Jun 26, 2002
Messages
3,511
Location
GB
I want to add an auto incrementing Row Id to my query. Such that if query returns 10 records. I want each numbered from 1 to 10.

Is this possible within a standard select statement?

Thank you

Ian
 
Sure,

SQL Server 2005+

select myField, row_number() over (order by SomeField) as Row from myTable

SQL Server 2000

select *, int(identity) as Row into #TempTable from myTable order by SomeField

select * from #TempTable

drop table #TempTable

PluralSight Learning Library
 
Thanks for this.

I looked on my TRANSACT SQL reference and there is no mention of row_number().

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top