Get a rowid in a SELECT statement

Posted on 2003-02-25
Medium Priority
Last Modified: 2012-06-21
Is it possible to get a row index column in the result of a SELECT which always starts with 1 and increment upwards, irrespective of the parameters in WHERE and ORDER BY clauses?

Question by:thomasgeorge
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
LVL 41

Expert Comment

ID: 8021572
In which database system ?

Expert Comment

ID: 8022160
if you are in oracle, you can use ROWNUM.
such as:
select rownum,ename,sal from emp
select rownum,ename,sal from emp where rownum<=5
select rownum,ename,sal from emp where rownum<=1

rownum start with 1, to the number of the records.

you can use it in WHERE(=1, <=) Order(you can use, does not make sense).


Expert Comment

ID: 8022466
I am not sure which database you using, but if you are using Microsoft SQL you could use a cursor.  Depending on the size of the table it could take a bit to run through the records.  Basically you would create a cursor to run through all of the records and put into a temp table.  In addition, you would have a variable that incriments by one for each record that it counts.

create newtable (put in fields and sizes, include one for the row number)

declare cursor_name scroll cursor
for select field1 from table_name

open cursor_name

declare @row_number numeric
delcare @field_name char(10)

set @row_number = 1

while @row_number <= @@cursor_rows
fetch next from cursor_name into @field_date

insert into newtable values (@row_number, @field_name)

set @row_number = @row_number +1

close cursor_name

deallocate cursor_name
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


Author Comment

ID: 8027316
I am using SQL Server. Is there a way to do this without a temporary table?

Author Comment

ID: 8027470
I am using SQL Server. Is there a way to do this without a temporary table?

Author Comment

ID: 8027476
I am using SQL Server. Is there a way to do this without a temporary table?

Accepted Solution

Tyip earned 100 total points
ID: 8031123
No, the only solution is to use a temp table, but you can do this:

SELECT RowNum = IDENTITY(int, 1, 1), *
INTO NewTable
FROM OldTable
where ...
order by ...

select into will lock your tempdb until the transaction is complete. If this is a problem, then create your NewTable with RowNum int identity(1,1) and insert into NewTable select * from OldTable where ...


Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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