SQL | MERGE Statement
SQL | MERGE Statement
Prerequisite – INSERT, UPDATE, DELETE
The MERGE command in SQL is actually a combination of three SQL statements: INSERT, UPDATE and DELETE. In simple words, the MERGE statement in SQL provides a convenient way to perform all these three operations together which can be very helpful when it comes to handle the large running databases. But unlike INSERT, UPDATE and DELETE statements MERGE statement requires a source table to perform these operations on the required table which is called as target table.
Now we know that the MERGE in SQL requires two tables : one the target table on which we want to perform INSERT, UPDATE and DELETE operations, and the other one is source table which contains the new modified and correct data for target table and is actually compared with the actual target table in order to modify it.
In other words, the MERGE statement in SQL basically merges data from a source result set to a target table based on a condition that is specified. The syntax of MERGE statement can be complex to understand at first but its very easy once you know what it means.So,not to get confused first let’s discuss some basics. Suppose you have two tables: source and target, now think if you want to make changes in the required target table with the help of provided source table which consists of latest details.
- When will you need to insert the data in the target table?
Obviously when there is data in source table and not in target table i.e when data not matched with target table. - When will you need to update the data?
When the data in source table is matched with target table but any entry other than the primary key is not matched. - When will you need to delete the data?
When there is data in target table and not in source table i.e when data not matched with source table.
Now, we know when to use INSERT, UPDATE and DELETE statements in case we want to use MERGE statement so there should be no problem for you understanding the syntax given below :
//.....syntax of MERGE statement....//
//you can use any other name in place of target
MERGE target_table_name AS TARGET
//you can use any other name in place of source
USING source_table_name AS SOURCE
ON condition (for matching source and target table)
WHEN MATCHED (another condition for updation)
//now use update statement syntax accordingly
THEN UPDATE
WHEN NOT MATCHED BY TARGET
//now use insert statement syntax accordingly
THEN INSERT
WHEN NOT MATCHED BY SOURCE
THEN DELETE;
That’s all about the MERGE statement and its syntax.
References –
MERGE – docs.microsoft
MERGE – docs.oracle
This article is contributed by Dimpy Varshni If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-08-02 Edit and replay XHR chrome/firefox etc?
2018-08-02 IIS application pool access desktop denied
2017-08-02 SQL Source Control
2017-08-02 3线-8线译码器