7

In Oracle, one can insert multiple rows by doing a query like

INSERT ALL
   INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
   INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
   INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;

And with a prepared statement, doing a single insert like this one

BEGIN INSERT 
   INTO mytable (column1, column2, column3) VALUES (null, 'val1.2', 'val1.3')
RETURNING column1 INTO ?; END;

will result in returning column1's value (supposing there is a trigger assigning a value to it before insert).

Is there a way, if possible at all, to combine both? Meaning, inserting multiple values while still returning all the column1 values (a resultset) with a single query?

1
  • The BULK COLLECT INTO synthax is not supported for INSERT as of 10gR2 (example here). I've not tested it with more recent versions of Oracle. Commented Apr 28, 2011 at 8:32

1 Answer 1

4

From the documentation (at least through the 21c version), one of the restrictions to the returning clause:

You cannot specify the returning_clause for a multitable insert.

Sign up to request clarification or add additional context in comments.

6 Comments

perhaps, but there should be way to execute a block and return a resultset of all the single inserts
if you need the individual returning into values for each insert statement, why not just run them, well, individually?
I was trying to avoid creating a new query (and request to the db) for every row and use some batch command... I guess a solution would be lock the table, insert every row in a block statement and then sending a query to fetch the new generated keys (the last n inserted rows) and finally unlocking the table. Does this sound right?
choosing your answer as accepted to close this question. Thanks!
Multiple insert queries is slow, I agree with Yanick
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.