The MERGE Statement

The new MERGE statement in SQL Server 2008 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. That’s right—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 new T-SQL feature), as you’ll see later in this chapter.

In earlier versions of SQL Server, separate multiple statements were required to achieve what can now be accomplished with a single MERGE statement. This new statement has a flexible syntax that allows us to exercise fine control over source and ...

Get Programming Microsoft® SQL Server™ 2008 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.