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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With