可以在 Oracle 的 SELECT 中执行自动编号序列吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4792572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
It is possible to do a autonumber sequence in a SELECT on Oracle?
提问by André
I need to do a task in Oracle that I don't know how can I possible do this.
我需要在 Oracle 中执行一项任务,但我不知道如何才能做到这一点。
Ok, I need to do a SELECT when I define a autonumber sequence on-the-fly.
好的,我需要在即时定义自动编号序列时执行 SELECT。
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?
这有可能吗?是否有任何 oracle 内置功能可以帮助我做到这一点?
采纳答案by René Nyffenegger
select
rownum
from
dba_objects,
dba_objects
where
rownum <= 9000;
回答by Alex Poole
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:
如果您想要一个独立于实际表中行的数字序列,而不是对返回的行进行编号(在这种情况下查看rownum
或row_number()
),您可以执行以下操作:
select level as auto
from dual
connect by level <= 9000;
回答by RichardTheKiwi
You can use Oracle's built in rownum
您可以使用 Oracle 的内置 rownum
select rownum as auto, other1, other2 from some_table
For ANSI compliance, you can use ROW_NUMBER() for later versions of Oracle
为了符合 ANSI,您可以将 ROW_NUMBER() 用于更高版本的 Oracle