Published on

May 31, 2010

Why You Should Use the MERGE Operation in SQL Server

Have you ever found yourself writing separate statements to INSERT, UPDATE, or DELETE data based on certain conditions in SQL Server? If so, then you need to know about the MERGE operation. In this blog post, we will explore the necessity of using the MERGE operation and how it can simplify your data manipulation tasks.

The MERGE operation is a new feature in SQL Server that provides an efficient way to perform multiple DML (Data Manipulation Language) operations. In earlier versions of SQL Server, developers had to write separate statements for each data manipulation operation. However, with the MERGE statement, you can include the logic for these data changes in one statement.

One of the most important advantages of the MERGE statement is that it reads and processes the entire data only once. In earlier versions, you had to write three different statements to process three different activities (INSERT, UPDATE, or DELETE). But with the MERGE statement, all the update activities can be done in one pass of the database tables.

Let’s take a look at an example to understand how the MERGE operation works:

USE tempdb
GO

CREATE TABLE StudentDetails
(
StudentID INTEGER PRIMARY KEY,
StudentName VARCHAR(15)
)
GO

INSERT INTO StudentDetails
VALUES(1,'SMITH')
INSERT INTO StudentDetails
VALUES(2,'ALLEN')
INSERT INTO StudentDetails
VALUES(3,'JONES')
INSERT INTO StudentDetails
VALUES(4,'MARTIN')
INSERT INTO StudentDetails
VALUES(5,'JAMES')
GO

CREATE TABLE StudentTotalMarks
(
StudentID INTEGER REFERENCES StudentDetails,
StudentMarks INTEGER
)
GO

INSERT INTO StudentTotalMarks
VALUES(1,230)
INSERT INTO StudentTotalMarks
VALUES(2,255)
INSERT INTO StudentTotalMarks
VALUES(3,200)
GO

-- Merge Statement
MERGE StudentTotalMarks AS stm
USING (SELECT StudentID, StudentName FROM StudentDetails) AS sd
ON stm.StudentID = sd.StudentID
WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
WHEN NOT MATCHED THEN
INSERT(StudentID, StudentMarks)
VALUES(sd.StudentID, 25);
GO

-- Select from Table
SELECT *
FROM StudentDetails
GO
SELECT *
FROM StudentTotalMarks
GO

-- Clean up
DROP TABLE StudentDetails
GO
DROP TABLE StudentTotalMarks
GO

In this example, we have two tables: StudentDetails and StudentTotalMarks. The MERGE statement is used to update the StudentTotalMarks table based on the conditions specified. It checks if the StudentMarks is greater than 250, and if so, it deletes the record. If the StudentMarks is not greater than 250, it updates the StudentMarks by adding 25. If there is no match, it inserts a new record with a StudentMarks value of 25.

Now, let’s take a look at the execution plan for the MERGE operator:

Merge Execution Plan

As you can see from the execution plan, the Number of Executions property suggests a value of 1. This means that the MERGE operation completes the operations of Insert, Update, and Delete in a single pass. This efficiency makes the MERGE operation a powerful tool for data manipulation.

I strongly recommend using the MERGE operation in your development, especially in data warehousing applications. It simplifies your code and improves performance by reducing the number of database reads and writes.

So, the next time you find yourself writing separate statements for data manipulation, consider using the MERGE operation in SQL Server. It will save you time and effort, and make your code more efficient.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.