t-sql-upserts-merge
Installation
SKILL.md
Write an upsert that survives concurrency, and refuse the dangerous MERGE
An upsert looks like a one-statement problem and is a concurrency problem. Both of the shapes most
code reaches for first, IF EXISTS ... UPDATE ELSE INSERT and a bare MERGE, are races. They
pass every single-session test and fail the first time two callers arrive together.
Open the concurrency and MERGE checks to rerun any result below.
Scope
- Key and index design belongs to
design-azure-sql-schema. - General T-SQL dialect belongs to
t-sql-correctness.
What the race actually costs
Run each pattern from several sessions at once, many increments each, on one hot key. A correct upsert ends at the arithmetic total.