Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing Select Columns in Oracle INSERT ALL Statement

I am using an INSERT ALL statement in Oracle to insert data into two different tables. The data is coming from a rather complex subquery rather than a VALUES clause.

My issue is that I don't want to insert all the columns returned by the subquery into both tables.

As a simple example, suppose I have two tables tab1 and tab2 each containing three columns col1, col2 and col3.

Now suppose my subquery returns a single row with the values A, B and C.

So, I want A, B and C inserted into col1, col2 and col3 of tab1 respectively, but I want, say, only A and C to go into col1 and col3 of tab2.

My statement would look something like this:

INSERT ALL
INTO tab1
  (col1, col2, col3)
INTO tab2
  (col1, ?, col3)
FROM
( ...
 complex subquery which returns A, B, C
  ...
)

Is there a way I can use some sort of a 'filler' or 'garbage column' to allow me to achieve my objective?

like image 997
Isaac Kleinman Avatar asked May 31 '26 11:05

Isaac Kleinman


2 Answers

Yes:

INSERT ALL
INTO tab1
  (col1, col2, col3) values (a, b, c)
INTO tab2
  (col1, col3) values (a, c)
SELECT a, b, c
FROM
( ...
 complex subquery which returns A, B, C
  ...
)
like image 189
Tony Andrews Avatar answered Jun 02 '26 03:06

Tony Andrews


this syntax is possible

INSERT ALL
INTO ap_cust(f1, f2) VALUES (a, d)
INTO ap_orders(f1, f2, f3) VALUES (a, b, c)
SELECT a, b, c,d
FROM table;

see http://psoug.org/reference/insert.html

like image 37
Raphaël Althaus Avatar answered Jun 02 '26 04:06

Raphaël Althaus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!