Sub topic 14
Loading several tables with INSERT ALL and INSERT FIRST
A multitable insert reads a source query once and writes its rows into more than one table in a single statement. Use it when a load would otherwise need several INSERT ... SELECT statements that scan the same source repeatedly.
This chapter assumes you can write INSERT ... SELECT, understand WHERE conditions, and know how COMMIT and ROLLBACK work. It uses emp_demo from the setup script at the start of this tutorial.
The shape of a multitable insert
Every multitable insert has two parts: one or more INTO clauses that name the targets, and one subquery that supplies the rows. The subquery runs once. For each row it returns, Oracle evaluates the INTO clauses and decides where the row goes.
There are three forms:
| Form | Keyword | Row goes to |
|---|---|---|
| Unconditional | `INSERT ALL` with no `WHEN` | Every `INTO` clause |
| Conditional all | `INSERT ALL` with `WHEN` | Every `INTO` whose condition is true |
| Conditional first | `INSERT FIRST` with `WHEN` | Only the first `INTO` whose condition is true |
Unconditional INSERT ALL
Without WHEN clauses, each source row is inserted into every target. This is a common way to split one wide source into several narrower tables.
CREATE TABLE emp_names AS SELECT emp_id, emp_name FROM emp_demo WHERE 1 = 0;
CREATE TABLE emp_pay AS SELECT emp_id, salary FROM emp_demo WHERE 1 = 0;
INSERT ALL
INTO emp_names (emp_id, emp_name) VALUES (emp_id, emp_name)
INTO emp_pay (emp_id, salary) VALUES (emp_id, salary)
SELECT emp_id, emp_name, salary
FROM emp_demo;The names inside VALUES (...) are not column names of the target. They refer to columns in the subquery's select list. If you use an expression in the subquery, give it an alias and refer to the alias.
Conditional INSERT ALL
Adding WHEN conditions lets each target pick the rows it wants. With ALL, Oracle tests every condition for every row, so one row can be inserted into several tables.
CREATE TABLE emp_high AS SELECT emp_id, salary FROM emp_demo WHERE 1 = 0;
CREATE TABLE emp_eng AS SELECT emp_id, emp_name FROM emp_demo WHERE 1 = 0;
INSERT ALL
WHEN salary >= 10000 THEN
INTO emp_high (emp_id, salary) VALUES (emp_id, salary)
WHEN dept_id = 20 THEN
INTO emp_eng (emp_id, emp_name) VALUES (emp_id, emp_name)
SELECT emp_id, emp_name, dept_id, salary
FROM emp_demo;An engineer who earns 12,000 lands in both emp_high and emp_eng. An employee who matches neither condition is not inserted anywhere, and no error is raised.
INSERT FIRST
INSERT FIRST evaluates the WHEN clauses in the order you wrote them and stops at the first true one. Each row therefore goes to at most one target, which makes the order of the clauses significant. An optional ELSE clause catches rows that matched nothing.
CREATE TABLE emp_regular AS SELECT emp_id, salary FROM emp_demo WHERE 1 = 0;
INSERT FIRST
WHEN salary >= 10000 THEN INTO emp_high (emp_id, salary) VALUES (emp_id, salary)
ELSE INTO emp_regular (emp_id, salary) VALUES (emp_id, salary)
SELECT emp_id, salary FROM emp_demo;Ordering matters
Because FIRST stops early, put the most specific condition first. Suppose you route by salary band:
CREATE TABLE emp_band_a AS SELECT emp_id, salary FROM emp_demo WHERE 1 = 0;
CREATE TABLE emp_band_b AS SELECT emp_id, salary FROM emp_demo WHERE 1 = 0;
INSERT FIRST
WHEN salary >= 10000 THEN INTO emp_band_a (emp_id, salary) VALUES (emp_id, salary)
WHEN salary >= 5000 THEN INTO emp_band_b (emp_id, salary) VALUES (emp_id, salary)
SELECT emp_id, salary FROM emp_demo;A salary of 12,000 satisfies both conditions but goes only to emp_band_a. If you swapped the two WHEN lines, every salary of 5,000 or more would go to emp_band_b and emp_band_a would stay empty. With INSERT ALL, the same two conditions would put the 12,000 row in both tables.
NULLs and unmatched rows
A WHEN condition that evaluates to unknown is treated as not true. A row with salary equal to NULL fails salary >= 10000. In the INSERT FIRST ... ELSE example above, that row falls through to emp_regular. In a statement without ELSE, it is silently dropped.
If you must not lose rows, add an ELSE target or a final catch-all condition such as WHEN 1 = 1. After the load, compare the number of source rows with the rows written.
Pivoting columns into rows
A multitable insert can turn repeating columns into rows by naming the same target several times. Each INTO writes one output row per source row.
CREATE TABLE weekly_sales (
emp_id NUMBER,
mon_amt NUMBER,
tue_amt NUMBER,
wed_amt NUMBER
);
CREATE TABLE daily_sales (
emp_id NUMBER,
day_name VARCHAR2(3),
amount NUMBER
);
INSERT ALL
INTO daily_sales (emp_id, day_name, amount) VALUES (emp_id, 'MON', mon_amt)
INTO daily_sales (emp_id, day_name, amount) VALUES (emp_id, 'TUE', tue_amt)
INTO daily_sales (emp_id, day_name, amount) VALUES (emp_id, 'WED', wed_amt)
SELECT emp_id, mon_amt, tue_amt, wed_amt
FROM weekly_sales;One weekly_sales row now produces three daily_sales rows. Add WHEN mon_amt IS NOT NULL THEN before each INTO if you want to skip empty days.
Counting what was inserted
The row count reported for a multitable insert is the total number of rows inserted across all targets, not the number of source rows. The unconditional example above reports twice the source row count because every row is written twice.
BEGIN
INSERT ALL
INTO emp_names (emp_id, emp_name) VALUES (emp_id, emp_name)
INTO emp_pay (emp_id, salary) VALUES (emp_id, salary)
SELECT emp_id, emp_name, salary FROM emp_demo;
DBMS_OUTPUT.PUT_LINE('Rows inserted in total: ' || SQL%ROWCOUNT);
ROLLBACK;
END;
/Restrictions and failure behavior
- The statement is atomic. If one target rejects a row, for example with ORA-00001, the whole statement is rolled back, including rows already written to other targets. Statement-level rollback does not undo earlier statements in your transaction.
- Targets must be tables. Views, materialized views, and remote tables are not allowed.
- The statement can name at most 999 target columns across all
INTOclauses. - A sequence's
NEXTVALis evaluated once per source row. EveryINTOclause that references it for the same row receives the same number. - Triggers on the target tables fire as usual. Row-level triggers see each inserted row separately.
Choosing between ALL and FIRST
Use INSERT ALL when the targets are independent copies or projections of the same row, such as splitting columns or pivoting. Use INSERT FIRST when the targets are mutually exclusive categories, such as salary bands or regions, and you want one row to land in exactly one place. If conditions can overlap and you want exactly one destination, FIRST with ordered conditions is simpler and safer than writing mutually exclusive conditions for ALL.
Exercise
A source table has 100 rows. You run INSERT ALL with two unconditional INTO clauses that target different tables. SQL%ROWCOUNT reports 200. Then you change the statement to INSERT FIRST with WHEN salary >= 10000 for the first INTO and WHEN dept_id = 20 for the second INTO. Thirty rows satisfy both conditions, 20 satisfy only the salary condition, 10 satisfy only the department condition, and 40 satisfy neither. What does SQL%ROWCOUNT report, and what happens to the 40 rows that match neither condition?
Show answer
SQL%ROWCOUNT reports 60. Each row goes to at most one target: the 30 rows matching both conditions and the 20 matching only the salary condition go to the first table (50 rows), and the 10 matching only the department condition go to the second table. The 40 rows that match neither condition are not inserted anywhere, and no error is raised, because the statement has no ELSE clause.