How to Format SQL MERGE Statements: Clean Multi-Condition Layout
By Sharon Ben-Moshe · October 12, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Format SQL MERGE statements for readability in SQL Server, Oracle, and BigQuery. Align MATCHED and NOT MATCHED clauses with clean indentation.
Quick answer
Format SQL MERGE statements by separating the target and source tables onto distinct lines, indenting the ON match predicate, and aligning each 'WHEN MATCHED' and 'WHEN NOT MATCHED' branch at the primary clause indentation level. In SQL Server (T-SQL), always remember that the entire MERGE statement must end with a mandatory semicolon.
FAQs
Why is a semicolon mandatory after MERGE in SQL Server?
In Transact-SQL, the semicolon is required to terminate MERGE statements (error Msg 10713 occurs if omitted) to prevent parsing ambiguity with subsequent CTEs or statements.
Can a MERGE statement perform DELETE operations?
Yes! A MERGE statement can include `WHEN MATCHED AND target.is_active = false THEN DELETE`, enabling simultaneous INSERT, UPDATE, and DELETE actions in a single statement.
Does PostgreSQL support the MERGE statement?
Yes! PostgreSQL added the standard ANSI MERGE statement in PostgreSQL 15, matching the capabilities found in Oracle, BigQuery, and SQL Server.
What happens if the source table has duplicate match keys?
If multiple source rows match a single target row, the MERGE statement aborts with an error because the target row cannot be updated multiple times within a single atomic statement.
Keep reading
Building an Enterprise SQL Style Guide: Formatting Standards, Linting, and Team Conventions
By Sharon Ben-Moshe · Oct 13, 2026
The complete blueprint for an enterprise SQL style guide. Standardize formatting, keyword casing, indentation, naming conventions, and CI linting.
Read postThe Master Guide to SQL Common Table Expressions (CTEs): Syntax, Optimization Fences, and Recursion
By Sharon Ben-Moshe · Oct 13, 2026
The definitive guide to SQL Common Table Expressions (CTEs). Master chaining, recursive hierarchies, data-modifying CTEs, and optimization fences.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.