Request multiple NEXTVAL from a sequence in a single statement

Basically, I need to request about a thousand NEXTVAL from the sequence. I can query them in a loop, or I can query them through a connection to a reeeeeally large table.

Is there an even less hacky way?

Upd. Basically, I have a schedule of operations on objects. Each object has either a generated UUID or an identifier from the database. After calculating the optimal schedule, I need to write it to the database, but each identifier in the table should be from a sequence. Therefore, I need to request some identifiers from this sequence. The problem is that the circular query is slow, because the database is really far from me, and I can't just lose a few seconds by executing dozens of queries in a loop. Therefore, I need to request all of these new IDs in one request.

+19
sql oracle
Nov 28 '11 at 6:25
source share
3 answers

You can use this:

select your_sequence.nextval from ( select level from dual connect by level < 1000 ); 
+31
Nov 28 '11 at 8:10
source share

Depends on what you want to do with them.

If you insert them into a table, you can cancel seq.nexval in the insert query. (As explained here: How to insert multiple rows in oracle with sequence value? )

If you use them in a loop, you can get them in this loop.

What do you want to do with them?

As I know, you cannot get multiple values ​​from a sequence.

UPDATE: a_horse_with_no_name aswer can be improved as follows:

 select your_sequence.nextval from dual connect by level < 1000 

:)

+14
Nov 28 '11 at 7:16
source share
 select sequence_name.nextval from dual connect by level < number of values you want to print; 
-four
Sep 30 '14 at 20:25
source share



All Articles