SQL Server MERGE Statement
Last Updated :
21 Jun, 2024
SQL Server MERGE statement combines INSERT, UPDATE, and DELETE operations into a single transaction.
MERGE in SQL Server
The MERGE statement in SQL provides a convenient way to perform INSERT, UPDATE, and DELETE operations together, which helps handle the large running databases. But unlike INSERT, UPDATE, and DELETE statements MERGE statement requires a source t,ble.
Now we know that the MERGE in SQL requires two tables: one is the target table on which we want to perform INSERT, UPDATE, and DELETE operations, and the other one is the source table which contains the new modified and correct data for the target table and is compared with the actual target table to modify it.
Syntax
The MERGE statement syntax is:
MERGE INTO target_table
USING source_table
ON merge_condition
WHEN MATCHED THEN UPDATE SET column1 = value1, column2 = value2, ...
WHEN NOT MATCHED THEN INSERT (column1, column2, ...) VALUES (value1, value2, ...)
WHEN NOT MATCHED BY TARGET THEN DELETE;
SQL Server MERGE Statement Examples
Let's look at an example of the MERGE statement in SQL Server. First, let's create two tables PRODUCT_LIST (target table) and UPDATED_LIST (source table)
PRODUCT_LIST Table:
P_ID | P_NAME | P_PRICE |
---|
101 | COFFEE | 15.00 |
102 | BISCUIT | 20.00 |
UPDATED_LIST Table:
P_ID | P_NAME | P_PRICE |
---|
101 | COFFEE | 25.00 |
103 | CHIPS | 22.00 |
Query:
MERGE PRODUCT_LIST AS TARGET
USING UPDATED_LIST AS SOURCE
ON (TARGET.P_ID = SOURCE.P_ID)
WHEN MATCHED AND (TARGET.P_NAME <> SOURCE.P_NAME OR TARGET.P_PRICE <> SOURCE.P_PRICE)
THEN UPDATE SET TARGET.P_NAME = SOURCE.P_NAME, TARGET.P_PRICE = SOURCE.P_PRICE
WHEN NOT MATCHED BY TARGET
THEN INSERT (P_ID, P_NAME, P_PRICE)
VALUES (SOURCE.P_ID, SOURCE.P_NAME, SOURCE.P_PRICE)
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
Output
P_ID | P_NAME | P_PRICE |
---|
101 | COFFEE | 25.00 |
103 | CHIPS | 22.00 |
References - MERGE - docs.microsoftMERGE - docs.oracle
Similar Reads
SQL MERGE Statement SQL MERGE Statement combines INSERT, DELETE, and UPDATE statements into one single query. MERGE Statement in SQLMERGE statement in SQL is used to perform insert, update, and delete operations on a target table based on the results of JOIN with a source table. This allows users to synchronize two tab
4 min read
Insert Statement in MS SQL Server The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table.In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the help
4 min read
SQL SELECT INTO Statement The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
Insert Into Select statement in MS SQL Server The INSERT INTO SELECT statement in SQL Server is a versatile feature that enables you to efficiently copy data from one or more tables into another table. This functionality is essential for tasks such as data transfer, backup creation, and data merging.In this article, We will learn to Insert Into
4 min read
Upsert Operation in SQL Server In SQL Server, managing data efficiently is crucial, especially when working with real-time or large datasets. The upsert operation is a combination of the INSERT and UPDATE commands that allow you to insert new data or update existing records in a table, depending on whether a match is found.In thi
5 min read