> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lancedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MERGE INTO

> Atomically update, insert, and delete rows with SQL MERGE INTO.

Enterprise only

`MERGE INTO` applies the existing merge-insert operation from SQL. It matches
source rows against target keys and commits the selected target changes atomically.
The source can be a table or an aliased subquery.

```sql theme={"theme":{"light":"vitesse-light","dark":"catppuccin-mocha"}}
MERGE INTO destination AS t
USING source AS s
ON t.id = s.id
WHEN MATCHED THEN UPDATE SET value = s.value
WHEN NOT MATCHED THEN INSERT (id, value) VALUES (s.id, s.value);
```

## Supported clauses

```sql theme={"theme":{"light":"vitesse-light","dark":"catppuccin-mocha"}}
MERGE INTO <target> [AS <target_alias>]
USING <source_table_or_aliased_subquery>
ON <target_key> = <source_key> [AND ...]
[WHEN MATCHED [AND <predicate>] THEN UPDATE SET <column> = <source_expression> [, ...]]
[WHEN NOT MATCHED THEN INSERT (<column> [, ...]) VALUES (<source_expression> [, ...])]
[WHEN NOT MATCHED BY SOURCE [AND <target_predicate>] THEN DELETE]
```

Specify at least one action, with at most one clause of each kind. `NOT MATCHED
BY TARGET` is also accepted for insert. Qualify both sides of every `ON`
equality with the target or source name or alias. Multiple key equalities can be
joined with `AND`; other join conditions are refused. Merge keys cannot change.

An update changes only the supplied columns. Assignments and inserted values
can contain source expressions, evaluated before matching, but cannot read the
target or contain subqueries. Put source subqueries in `USING` instead.

When both update and insert are present, they must write the **same columns
and source expressions**. The update can omit unchanged merge keys; the insert
must explicitly include every key with its source value from `ON`. For example,
both arms can use `s.value + 1`, but using `s.value` in one arm and `0` in the
other is refused.

Update predicates can read qualified target columns and source columns carried
unchanged through that shared projection. Delete predicates can read only
qualified target columns. Conditional inserts, matched deletes, multiple clauses
of the same kind, and `OUTPUT` are unsupported.

```sql theme={"theme":{"light":"vitesse-light","dark":"catppuccin-mocha"}}
MERGE INTO destination t
USING (SELECT id, value FROM incoming WHERE value IS NOT NULL) s
ON t.id = s.id
WHEN MATCHED AND s.value > t.value THEN UPDATE SET value = s.value
WHEN NOT MATCHED THEN INSERT (id, value) VALUES (s.id, s.value)
WHEN NOT MATCHED BY SOURCE AND t.value < 0 THEN DELETE;
```

The delete clause applies to target rows with no matching source key. With an
empty source it can delete every target row satisfying its predicate.

## INSERT ON CONFLICT

`INSERT ... ON CONFLICT` uses the same atomic merge-insert operation. Specify
one or more conflict key columns, then update matching rows or leave them
unchanged. Unmatched rows are inserted in either case.

```sql theme={"theme":{"light":"vitesse-light","dark":"catppuccin-mocha"}}
INSERT INTO items (id, value) VALUES (1, 10), (2, 20)
ON CONFLICT (id) DO UPDATE SET value = EXCLUDED.value;

INSERT INTO items (id, value) VALUES (2, 200), (3, 30)
ON CONFLICT (id) DO NOTHING;

INSERT INTO items (id, value) SELECT id, value FROM incoming
ON CONFLICT (id) DO UPDATE SET value = EXCLUDED.value;
```

The column list is optional; omitting it uses the table's non-computed columns
in schema order. Source values are converted to their destination types before
matching conflict keys. Multiple key columns match together, as with `MERGE`.

The update and insert actions share one projection. Assign every inserted
non-key column using `SET col = EXCLUDED.col`; unchanged key columns can be
omitted from `SET`. Columns outside the INSERT column list remain unchanged
on matched rows. Existing-row expressions, different source columns in an
assignment, and `DO UPDATE ... WHERE` are unsupported. A bare `ON CONFLICT
DO NOTHING`, `ON CONFLICT ON CONSTRAINT`, and `RETURNING` are also unsupported.
There are no unique constraints from which to infer conflict keys.

Duplicate source keys follow `MERGE` behavior. On tables without a WAL, multiple
source rows updating the same target row fail atomically. Duplicate keys absent
from the target can insert multiple rows, including with `DO NOTHING`; the
operation does not enforce uniqueness or deduplicate the source. A NULL key
does not match another NULL key. On WAL tables, the existing read-on-write
merge path determines duplicate handling, just as for `MERGE`.

These forms require the same INSERT and UPDATE privileges as `MERGE`, including
`DO NOTHING`. They use the same WAL routing and computed-column protections
described below. The result reports operation `MERGE`, affected rows, and the
resulting table version; rows skipped by `DO NOTHING` are not affected rows.

## Types and identifiers

Merge keys and source columns read by a predicate must have the same data type
as their target projection columns. String representations (`Utf8`, `LargeUtf8`,
`Utf8View`) can convert between one another, as can binary representations.
Other implicit conversions on these columns are refused before writing because
casting could change a match or predicate result. Write-only values use the
normal write casts.

Choose a conversion explicitly in `USING` when needed. For example, casting a
floating-point key to `BIGINT` there makes the converted key part of the SQL
comparison:

```sql theme={"theme":{"light":"vitesse-light","dark":"catppuccin-mocha"}}
MERGE INTO destination t
USING (SELECT CAST(id AS BIGINT) AS id, value FROM incoming) s
ON t.id = s.id
WHEN MATCHED THEN UPDATE SET value = s.value;
```

Names follow the session's [SQL identifier rules](https://docs.lancedb.com/enterprise/v1-16-0/sql/identifiers.md): by default,
unquoted names are lowercased and quoted names preserve case. Thus `t.ID` reads
`id`, while `t."ID"` reads the distinct uppercase column. Disabling
`datafusion.sql_parser.enable_ident_normalization` preserves unquoted case too.

Computed columns, including Function outputs, cannot be written by `MERGE`.
A Function output cannot be a merge key. Fill those columns through
[`REFRESH COLUMN`](https://docs.lancedb.com/enterprise/v1-16-0/sql/dml/refresh#refresh-column).

## WAL tables

On a table with a write-ahead log (WAL) write specification, `MERGE` uses the
existing bounded read-on-write merge path. Its `ON` keys must be exactly the
table's stored, indexed primary key. Predicates and `NOT MATCHED BY SOURCE
DELETE` are refused. Source data must fit the WAL request body limit; an
oversized request fails instead of falling back to a direct table write.

## Result

The DML result reports operation `MERGE`, the total number of updated, inserted,
and deleted rows, and the resulting table version.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.