How to Upsert in SQL Server (MERGE or UPDATE + INSERT)

Last updated July 25, 2026 · By the SaturnSQL team

SQL Server has no ON CONFLICT. Use MERGE, or an UPDATE followed by a conditional INSERT in one transaction. If you use MERGE, add WITH (HOLDLOCK) so concurrent upserts of the same key cannot race.

MERGE

MERGE customers WITH (HOLDLOCK) AS target
USING (VALUES (101, '[email protected]')) AS source (id, email)
  ON target.id = source.id
WHEN MATCHED THEN
  UPDATE SET email = source.email
WHEN NOT MATCHED THEN
  INSERT (id, email) VALUES (source.id, source.email);

UPDATE then INSERT

BEGIN TRANSACTION;

UPDATE customers WITH (UPDLOCK, SERIALIZABLE)
SET email = '[email protected]'
WHERE id = 101;

IF @@ROWCOUNT = 0
  INSERT INTO customers (id, email)
  VALUES (101, '[email protected]');

COMMIT;

MERGE has a long history of edge-case bugs and surprising trigger behavior, so many teams standardize on the two-statement pattern. For single-row upserts both perform fine; test MERGE carefully before using it in bulk loads.

Run this in SaturnSQL

SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.

Try it free

Related SQL Server guides

© 2026 Panda Capital Oy Ab. All rights reserved.