The INSERT ALL statement evaluates all WHEN conditions for each row from the source query.
If multiple conditions are true for a single row, that row will be inserted into multiple tables.
Example:
INSERT ALL
WHEN id1 = 1 THEN INTO table_insert_2
WHEN id1 = 2 THEN INTO table_insert_3
ELSE INTO table_insert_4
SELECT id1, desc1 FROM table_insert_1;
-
Select data from the table "
table_insert_2
":
SELECT id1, desc1 FROM table_insert_2;
Query result:
id1 desc1
----- -----
1 A
-
Select data from the table "
table_insert_3
":
SELECT id1, desc1 FROM table_insert_3;
Query result:
id1 desc1
----- -----
2 B
-
Select data from the table "
table_insert_4
":
SELECT id1, desc1 FROM table_insert_4;
Query result:
id1 desc1
----- -----
3 C
Note:
All WHEN clauses are evaluated for each row returned by the subquery.
If multiple conditions evaluate to true for the same row, the insert statements will be executed for all matching WHEN clauses.
This can result in the same data being inserted into multiple tables.
To illustrate this behavior, consider the following example with overlapping conditions:
INSERT ALL
WHEN id1 < 2 THEN INTO table_insert_2
WHEN id1 < 3 THEN INTO table_insert_3
ELSE INTO table_insert_4
SELECT id1, desc1 FROM table_insert_1;
-
Select data from the table "
table_insert_2
":
SELECT id1, desc1 FROM table_insert_2;
Query result:
id1 desc1
----- -----
1 A
-
Select data from the table "
table_insert_3
":
SELECT id1, desc1 FROM table_insert_3;
Query result:
id1 desc1
----- -----
1 A
2 B
-
Select data from the table "
table_insert_4
":
SELECT id1, desc1 FROM table_insert_4;
Query result:
id1 desc1
----- -----
3 C