Multi-Table Insert
Read one source once and insert its rows into several tables. Run it in a SQL paragraph, or in a Magpie paragraph wrapped in sql(…). See Changing Table Data with SQL for where DML runs, which tables accept it, and worked examples.
Syntax
FROM <relation>
INSERT { INTO | OVERWRITE } [TABLE] <table> [PARTITION (<partition column> = <value> [, ...])] [(<column> [, ...])]
[REPLACE WHERE <predicate>]
SELECT <columns> [WHERE ...] [GROUP BY ...] [ORDER BY ...] [LIMIT <n>]
[INSERT ...]
[...]
Parameters
relation
String — The table to read. Every insert reads from it.
Each INSERT
An insert into one table, with the same clauses and rules as Insert Into, Insert Overwrite, or Insert Replace Where. Each SELECT reads from the relation and must not have its own FROM. A statement with a single INSERT is an ordinary insert.
How it runs
- Magpie checks every target table and analyzes every
SELECTbefore writing anything. If any insert is invalid, nothing is written. - The inserts then run one at a time, in order. They are not one transaction: if insert n fails, inserts 1 to n−1 stay written, and the error says how many had been written.
Supported tables
Each insert follows the rules of its own statement: see Which tables you can change.
Examples
-- route staged orders into per-status tables and rebuild a summary partition,
-- reading the source once
FROM staging.orders
INSERT INTO sales.fulfilled_orders
SELECT order_id, customer_id, order_total WHERE status = 'FULFILLED'
INSERT INTO sales.cancelled_orders
SELECT order_id, customer_id, cancellation_reason WHERE status = 'CANCELLED'
INSERT OVERWRITE analytics.order_status_summary PARTITION (load_date = '2026-07-22')
SELECT status, count(*) AS order_count GROUP BY status
Errors
Besides the errors of each insert's own statement:
| Message | Cause |
|---|---|
Multi-insert failed while executing INSERT <n> of <total> (target "<table>"); the first <n−1> INSERT(s) of the statement had already been written: <cause> | Insert n failed while writing; the earlier inserts are written. When n is 1, the message says no earlier insert had been written. |
A multi-insert select body cannot contain its own FROM clause | A SELECT has its own FROM. |
INSERT OVERWRITE DIRECTORY is not supported in a multi-insert statement | An insert writes to a directory. |
Security
Requires Write permission on every target table and Read permission on every table the statement reads.