SQL

SQL Merge and Upsert Generator

Builds an upsert statement from a table definition and the key columns you nominate.

Loading the tool…

Processing happens locally in your browser. What you paste or load is processed by this page and is not uploaded to a server. Nothing is stored unless you use a control that says it stores something, and you can clear anything this site has kept from the privacy page.

How to use this tool

  1. Paste the CREATE TABLE statement for the target table.
  2. Name the staging table and the key columns to match on, or leave them blank to use the primary key.
  3. Select Generate upsert, read the statement, and test it against a copy first.

What sql merge generator does

An upsert inserts rows that are new and updates rows that already exist, matched on a key. Paste the target table’s DDL, name the key columns and the staging table, and this writes the statement: ON CONFLICT DO UPDATE for PostgreSQL, and a MERGE for the platforms that use it.

Two things decide whether an upsert behaves. The key columns must be genuinely unique in the source — two staging rows with the same key will make some platforms fail the whole statement — and there must be a unique constraint on those columns in the target. Read the generated statement, check the column list, and run it against a copy before it touches anything that matters.

Frequently asked questions

An INSERT ... ON CONFLICT (key) DO UPDATE SET ... statement, which is the PostgreSQL upsert. Other dialects get a MERGE statement with matched and not-matched branches.

Yes for the PostgreSQL form — ON CONFLICT needs a unique index or constraint on exactly those columns. For MERGE it is not strictly required, but without it duplicate matches can produce unpredictable results.

Treat it as a draft. It updates every non-key column unconditionally, which is usually what you want but will overwrite a column your pipeline deliberately leaves alone. Read it, adjust it, and test against a copy first.

Some platforms fail the whole statement, others pick a row without telling you. De-duplicate the source first — the key you match on has to be unique in the source, not just in the target.