The MERGE Statement

The MERGE statement does just what its name says. It combines the normal insert, update, and delete operations involved in a typical merge scenario, along with the select operation that provides the source and target data for the merge. Essentially, that means it combines four statements into one. In fact, you can combine five statements into one using the OUTPUT clause, and even more than that with INSERT OVER DML (a special T-SQL feature syntax, which we cover next).

Prior to SQL Server 2008, separate, multiple statements were required to achieve what can now be accomplished with a single MERGE statement. This statement has a flexible syntax that allows you to exercise fine control over source and target matching, as well ...

Get Programming Microsoft® SQL Server® 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.