Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence next value in SELECT statement

Tags:

sql

oracle

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.

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

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.

like image 275
Mr.Maze Avatar asked Oct 03 '16 18:10

Mr.Maze


People also ask

How to select the next value from a sequence object?

If you want to select the next value from sequence object, you can use this SQL statement. 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.

What is the difference between sequence&nextval and insert statements?

NEXTVAL statement is used to insert values on existing table by increasing old sequence value with increment by value and returns generated new value. SEQUENCE& NEXTVAL statements are used in ORACLE database. In SELECT list of SELECT statement that is not contained a subquery or a view. In SELECT list of subquery INSERT statement.

Where we can use sequence and nextval statements in Oracle?

SEQUENCE& NEXTVAL statements are used in ORACLE database. Where we can use SEQUENCE values – In VALUES clause of INSERT statement while inserting values in table; In SELECT list of SELECT statement that is not contained a subquery or a view. In SELECT list of subquery INSERT statement. In SET clause of UPDATE statement.

What is sequence&nextval with detailed example?

In this topic, we described about the SEQUENCE & NEXTVAL with detailed example. SEQUENCE & NEXTVAL statements are used to insert values automatically PRIMAR and UNIQUE columns while adding new rows on table. SEQUENCE statement is used to generate UNIQUE values on particular column in existing table with starting value and increment by value.


4 Answers

Don't use a sub-select:

SELECT col1, co2, col3, TEST_SEQ.NEXTVAL as SEQ
FROM table; 
like image 135
a_horse_with_no_name Avatar answered Oct 18 '22 11:10

a_horse_with_no_name


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

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.

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

like image 42
Mohammed Osman Avatar answered Oct 18 '22 11:10

Mohammed Osman


In tsql use Next Value for dbo.seqYourSequence to retrieve a value

like image 24
Golden Lion Avatar answered Oct 18 '22 11:10

Golden Lion


select SEQ_tablename.NEXTVAL from DUAL;
like image 41
Mugeesh Husain Avatar answered Oct 18 '22 11:10

Mugeesh Husain