In this article, we will be discussing about, the MERGE Statement in SQL Server. The MERGE Statement is a T-SQL enhancement, which was originally shipped with SQL Server 2008.
What is the MERGE Statement in SQL Server?
So what actually is the MERGE Statement? When and how should it be used?
The MERGE Statement actually allows joining a source with a target table and then based on the results of the join, it performs insert, update, or delete operations on the target table.
A simplified syntax example of the MERGE Statement is the following:
MERGE [tableA] as target using [tableB] as source on target.id = source.id
when matched then update set target.col1 = source.col1, target.col2 = source.col2
when not matched then insert values (source.id, source.col1, source.col2)
when not matched by source then delete;
So let’s analyze the above syntax. I will use some steps for that.
Step 1: Declare your source and target tables.
Step 2: Declare a joining condition on the two tables (in our example is the target.id = source.id condition).
Step 3: Set the action for the when matched case. This case means that a record was found that exists in both tables. To this end, you should perform an update in order to synchronize the two records.
Step 4: Set the action for the when not matched case. This case means that a record was found in the source table which does not exist in the target table. To this end, you should perform an insert in order to copy the record from the source to the target.
Step 5: Set the action for the when not matched by source case. This case means that a record was found in the target table which does not exist in the source table. To this end, you can delete this record (if the purpose of this operation is the synchronization of the two tables).
Please note that the MERGE statement should always end with a semicolon (;).
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
(Lifetime Access/ Live Demos / Downloadable Resources and more!)
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.
Views:2,924
1 thought on “The MERGE Statement in SQL Server”
Something I would also like to mention regarding the MERGE statement is that by adding in the statement the following code: OUTPUT $action, Inserted.*, Deleted.*; every time the MERGE statement is executed you will get information on which records were updated, inserted and deleted.
So based on this post's example the code can be modified as follows in order to include the OUTPUT clause:
————————— MERGE [tableA] as target using [tableB] as source on target.id = source.id when matched then update set target.col1 = source.col1, target.col2 = source.col2 when not matched then insert values (source.id, source.col1, source.col2) when not matched by source then delete OUTPUT $action, Inserted.*, Deleted.*; —————————
Comments are closed.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Read More
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Something I would also like to mention regarding the MERGE statement is that by adding in the statement the following code: OUTPUT $action, Inserted.*, Deleted.*; every time the MERGE statement is executed you will get information on which records were updated, inserted and deleted.
So based on this post's example the code can be modified as follows in order to include the OUTPUT clause:
—————————
MERGE [tableA] as target using [tableB] as source on target.id = source.id
when matched then update set target.col1 = source.col1, target.col2 = source.col2
when not matched then insert values (source.id, source.col1, source.col2)
when not matched by source then delete
OUTPUT $action, Inserted.*, Deleted.*;
—————————