Skip to main content

Insert Replace Where

Replace the rows of a Delta table that match a predicate with the rows of a query, in one transaction. Run it in a SQL paragraph, or in a Magpie paragraph as sql(INSERT INTO … REPLACE WHERE …). See Changing Table Data with SQL for where DML runs, which tables accept it, and worked examples.

Syntax​

INSERT INTO [TABLE] <table> REPLACE WHERE <predicate> <source>

<source> is a query, VALUES (…), or TABLE <source table>, as for Insert Into.

Parameters​

table​

String — The Delta table to write to, as table or schema.table, in the current repository.

predicate​

Expression — The rows to replace. Use unqualified column names.

source​

Query — The replacement rows, written positionally to the table's declared columns.

REPLACE WHERE can't be combined with INSERT OVERWRITE, a PARTITION clause, or a column list.

Supported tables​

Delta tables only.

Examples​

-- swap out all of July for the recomputed rows
INSERT INTO analytics.daily_totals REPLACE WHERE order_month = '2026-07'
SELECT order_month, order_date, sum(amount) AS total
FROM sales.orders
WHERE order_month = '2026-07'
GROUP BY order_month, order_date

Errors​

Besides the common DML errors:

MessageCause
REPLACE WHERE cannot be combined with INSERT OVERWRITEINSERT OVERWRITE … REPLACE WHERE.
PARTITION cannot be combined with REPLACE WHEREA PARTITION clause with REPLACE WHERE.
Explicit column list cannot be combined with REPLACE WHEREA column list with REPLACE WHERE.
<statement> is only supported on Delta tables for file-backed targets; "<table>" uses <format>.The table is on a file format other than Delta.
<statement> is not supported for table "<table>" (connector: <type>). The <type> Spark connector supports scans and batch writes, not the row-level operations Spark DML requires; row-level DML requires a Delta table.The table is connector-backed.
DML target '<table>' cannot be used as a column-reference qualifier (e.g., '<table>.col'). Use unqualified column names in the REPLACE WHERE predicate; …The predicate qualifies a column with the table name.

Security​

Requires Write permission on the table and Read permission on every table the source reads.