Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert multiple rows into oracle with a sequence value?

Tags:

sql

oracle

I know that I can insert multiple rows using a single statement, if I use the syntax in this answer.

However, one of the values I am inserting is taken from a sequence, i.e.

insert into TABLE_NAME (COL1,COL2) select MY_SEQ.nextval,'some value' from dual union all select MY_SEQ.nextval,'another value' from dual ; 

If I try to run it, I get an ORA-02287 error. Is there any way around this, or should I just use a lot of INSERT statements?

EDIT:
If I have to specify column names for all other columns other than the sequence, I lose the original brevity, so it's just not worth it. In that case I'll just use multiple INSERT statements.

like image 339
Ovesh Avatar asked Oct 23 '08 01:10

Ovesh


People also ask

How do I insert values into multiple rows?

Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.

How can I insert more than 1000 rows in Oracle?

The 1000 limit only applies when you are passing in the rows using a values statement - if you were inserting based on a select from a table then there is no limit. The row constructor, using VALUES, has a limit of up to 1000 rows. You can split the insert in two chuncks, or you can use SELECT ... UNION ALL instead.


1 Answers

This works:

insert into TABLE_NAME (COL1,COL2) select my_seq.nextval, a from (SELECT 'SOME VALUE' as a FROM DUAL  UNION ALL  SELECT 'ANOTHER VALUE' FROM DUAL) 
like image 141
WW. Avatar answered Sep 22 '22 01:09

WW.