Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to do a autonumber sequence in a SELECT on Oracle?

I need to do a task in Oracle that I don't know how can I possible do this.

Ok, I need to do a SELECT when I define a autonumber sequence on-the-fly.

For example:

Select autonumber(1, 9000) as auto from some_table

And the result would be

auto
------
1
2
3
4
5
6
7
8
9
10
...
9000

This would be possible to do? Are there any oracle build in function that will help me doing this?

like image 744
André Avatar asked Dec 02 '22 03:12

André


2 Answers

If you want a sequence of numbers independent of rows in an actual table, rather than numbering the returned rows (in which case look at rownum or row_number()), you can do:

select level as auto
from dual
connect by level <= 9000;
like image 106
Alex Poole Avatar answered Apr 28 '23 23:04

Alex Poole


You can use Oracle's built in rownum

select rownum as auto, other1, other2 from some_table

For ANSI compliance, you can use ROW_NUMBER() for later versions of Oracle

like image 26
RichardTheKiwi Avatar answered Apr 29 '23 00:04

RichardTheKiwi