Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / SQL

What is the Difference Between Merge and Update?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Feb 2017MIT3 min read 15.2K   2  
This post discusses the difference between merge and update

When modifying one table based on the contents of another, it may be tempting to simply use the merge statement, but before you throw away all other forms of modifying data, I think its important for you to know the difference between merge and update.

What is the Difference Between MERGE and UPDATE?

Both the MERGE and UPDATE statements are designed to modify data in one table based on data from another, but MERGE can do much more.

Whereas UPDATE can only modify column values you can use the MERGE statement to synchronize all data changes such as removal and addition of row. The MERGE statement is structured to handle all three operations, INSERT, UPDATE, and DELETE, in one command.

When you just need to UPDATE data youre better off using the UPDATE statement as the MERGE statement is built to handle several matching scenarios, it is more complex and may run less efficiently. Per MSDN:

The conditional behavior described for the MERGE statement works best when the two tables have a complex mixture of matching characteristics. For example, inserting a row if it does not exist, or updating the row if it does match. When simply updating one table based on the rows of another table, improved performance and scalability can be achieved with basic INSERT, UPDATE, and DELETE statements. (MSDN: MERGE Transact-SQL)

Difference Between Merge and Update

Here is a side-by-side comparison of the MERGE and UPDATE statements:

In this side by side comparison you can see the similarities key areas of these statements:

  1. Target – They both specify a target data source.
  2. Source “ Each has a source of information to drive the update.
  3. Merge Condition “ In both you find a means to match rows in one table to the next. In the MERGE statement, this is called the merge condition. In an INNER JOIN, it is called a join condition.
  4. Column Update – Each has a SET clause to specify which columns are updated.

Key Differences between MERGE and UPDATE

First Difference

With MERGE, youre able to combine update, delete, and insert command into one statement. This is because the MERGE statement uses a WHEN clause to determine the course of action to take on the match.

With a MERGE, you can take different actions based on the rows matching or not matching the target or source. With the updated, youre only updating rows that match.

Consider if you want to do synchronize all chance from one table to the next. In this case merge become more efficient as less passes through the data.

MERGE esqlProductTarget T
USING esqlProductSource S
ON (S.ProductID = T.ProductID)
WHEN MATCHED
THEN UPDATE
     SET T.Name = S.Name,
         T.ProductNumber = S.ProductNumber,
         T.Color = S.Color
WHEN NOT MATCHED BY TARGET
THEN INSERT (ProductID, Name, ProductNumber, Color)
     VALUES (S.ProductID, S.Name, S.ProductNumber, S.Color)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;

Second Difference

Another difference is I feel the MERGE statement is easier to read. Look again at the comparison above. I think the MERGE command œflows easier. Youre able to read it top down. The key items, such as source, target, merge conditions, and the set clause are in order top down.

When you read the UPDATE statement, the key items are scattered. And it takes a bit of training to understand the INNER JOIN.

Third Difference

The UPDATE statement will most likely be more efficient than a MERGE if the all you are doing is updating rows. Given the complex nature of the MERGE commands match condition, it can result in more overhead to process the source and target rows. However, when you need to do more than one operation, the MERGE command is most likely a better choice, as you are only making one pass through the data as opposed to multiple passes, one for each separate UPDATE, INSERT, or DELETE command, through the source data.

Want to learn more about UPDATE and Merge? If so, I would recommend ready my article Introduction to SQL Server Data Modification Statements.

The post What is the Difference Between Merge and Update? appeared first on Essential SQL.

This article was originally posted at https://www.essentialsql.com/difference-merge-update

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --