Skip to main content

Merge Into

Update, insert, and delete the rows of a Delta table by matching them against a source table or query. Run it in a SQL paragraph, or in a Magpie paragraph as sql(MERGE INTO …). See Changing Table Data with SQL for where DML runs, which tables accept it, and worked examples.

Syntax​

MERGE INTO <table> [[AS] <target alias>]
USING { <source table> | (<query>) } [[AS] <source alias>]
ON <condition>
<when clause> [<when clause> ...]

where each <when clause> is one of:

WHEN MATCHED [AND <condition>] THEN
{ UPDATE SET * | UPDATE SET <column> = <expression> [, ...] | DELETE }

WHEN NOT MATCHED [BY TARGET] [AND <condition>] THEN
{ INSERT * | INSERT [(<column> [, ...])] VALUES (<expression> [, ...]) }

WHEN NOT MATCHED BY SOURCE [AND <condition>] THEN
{ UPDATE SET <column> = <expression> [, ...] | DELETE }

Parameters​

table​

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

source table or query​

The rows to match against the table: a table, or a query in parentheses. The source can read any table you can read.

target alias and source alias​

String — Optional. Names for the table and the source within the statement. If you give the table an alias, qualify its columns with the alias, not the table name. Without a target alias, the table name works as a qualifier, unless the source alias is the same as the table name.

condition​

Expression — How a source row matches a table row.

When clauses​

At least one is required. They are checked in order, and the first whose AND condition holds applies:

  • WHEN MATCHED — a table row has a matching source row. UPDATE SET * copies every column from the source.
  • WHEN NOT MATCHED [BY TARGET] — a source row has no matching table row. INSERT * inserts every column from the source.
  • WHEN NOT MATCHED BY SOURCE — a table row has no matching source row.

Supported tables​

Delta tables only.

Examples​

-- upsert: update existing rows, insert new ones
MERGE INTO sales.customers t
USING staging.customer_updates s
ON t.customer_id = s.customer_id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *

-- synchronize with a snapshot: update stale rows, add new ones,
-- and delete US rows that disappeared from the source
MERGE INTO sales.customers t
USING (SELECT * FROM staging.customer_snapshot WHERE region = 'us') s
ON t.customer_id = s.customer_id
WHEN MATCHED AND s.updated_at > t.updated_at THEN
UPDATE SET name = s.name, email = s.email, updated_at = s.updated_at
WHEN NOT MATCHED BY TARGET THEN
INSERT (customer_id, name, email, updated_at)
VALUES (s.customer_id, s.name, s.email, s.updated_at)
WHEN NOT MATCHED BY SOURCE AND t.region = 'us' THEN DELETE

Errors​

Besides the common DML errors:

MessageCause
<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'). Attach an alias to the target and reference the alias instead …The table has an alias, but a column is qualified with the table name.

Security​

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

Lineage​

Describe on the table shows the merge's source tables as an operation node between the table and its sources.