SQL 在 SELECT 语句中对下一个值进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39838157/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:54:23  来源:igfitidea点击:

Sequence next value in SELECT statement

sqloracle

提问by Mr.Maze

I would like to use the same sequence number across multiple select statements in SQL form to eventually use it as a PK insert in a table and tie multiple records together.

我想在 SQL 形式的多个选择语句中使用相同的序列号,最终将其用作表中的 PK 插入并将多条记录联系在一起。

So far, I can only select the NEXTVAL from dual:

到目前为止,我只能从双中选择 NEXTVAL:

SELECT TEST_SEQ.NEXTVAL AS SEQ FROM DUAL;

However, when I include the sequence into a multiple column select, I get sequence not allowed here error.

但是,当我将序列包含在多列选择中时,出现此处不允许序列错误。

SELECT col1, co2, col3, (select TEST_SEQ.NEXTVAL) SEQ
FROM table; 

Any help is greatly appreciated.

任何帮助是极大的赞赏。

采纳答案by a_horse_with_no_name

Don't use a sub-select:

不要使用子选择:

SELECT col1, co2, col3, TEST_SEQ.NEXTVAL as SEQ
FROM table; 

回答by Mohammed Osman

If you want to select the next value from sequence object, you can use this SQL statement.

如果要从序列对象中选择下一个值,可以使用此 SQL 语句。

SELECT NEXT VALUE FOR [dbo].[seq_Vehicles] AS NextVehicleId

If you want to select multiple next values from SQL Sequence, you have to loop calling the above SQL statement and save the "next value" got in a storage.

如果要从 SQL 序列中选择多个下一个值,则必须循环调用上述 SQL 语句并将“下一个值”保存在存储中。

You can loop using (while loop) or by (cursor).

您可以使用 (while loop) 或 by (cursor) 进行循环。