Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query several NEXTVAL from sequence in one statement

Tags:

sql

oracle

Basically, I need to query about a thousand NEXTVAL from a sequence. I can query them in a loop, or I can query them through a join with a reeeeeally big table.

Is there any less hacky way?

Upd. Basically, I have a schedule of operations on objects. Each object has either a generated UUID, or an ID from database. After I calculate an optimal schedule, I need to write it into DB, but every ID in the table HAS to be from a sequence. So I need to query some IDs from that sequence. The problem is that looping query is slow, since the DB is really far from me, and I can't just loose several seconds while executing dozens of queries in a loop. So I need to query all those new IDs in one query.

like image 317
F0RR Avatar asked Nov 28 '11 06:11

F0RR


People also ask

How do you find the Nextval of a sequence?

If you want to select the next value from sequence object, you can use this SQL statement. 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. You can loop using (while loop) or by (cursor).

How do you use a sequence Nextval in a select statement?

You can access the value of a sequence using the NEXTVAL or CURRVAL operators in SQL statements. You must qualify NEXTVAL or CURRVAL with the name (or synonym) of a sequence object that exists in the same database, using the format sequence. NEXTVAL or sequence. CURRVAL.

Does Nextval increment the sequence?

When you create a sequence, you can define its initial value and the increment between its values. The first reference to NEXTVAL returns the sequence's initial value. Subsequent references to NEXTVAL increment the sequence value by the defined increment and return the new value.

How do I query sequence in SQL?

The syntax to a view the properties of a sequence in SQL Server (Transact-SQL) is: SELECT * FROM sys. sequences WHERE name = 'sequence_name'; sequence_name.


1 Answers

You can use this:

select your_sequence.nextval from (    select level     from dual     connect by level < 1000 ); 
like image 105
a_horse_with_no_name Avatar answered Sep 20 '22 16:09

a_horse_with_no_name