T-SQL 101: 142 Merging data into a SQL Server table
Sometimes, you want to insert a row if it’s missing but update it if it’s already there. We had asked Microsoft for an UPSERT statement as that’s what it’s called in other databases.
What we got in SQL Server 2008 instead was a MERGE statement. It’s more flexible than an UPSERT statement.
In the example above, I’ve said I want to merge into the dbo.CinemaGroups table. Note that this statement also has an optional INTO word, just like INSERT does. That’s then considered the target table. Only one table can be modified in a single query, including with a MERGE query.
2025-04-09