https://blog.csdn.net/CrazyMo_/article/details/46010325 https://docs.microsoft.com/zh-cn/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15

Merge 语法解释:

MERGE 语句是首先对源表和目标表都进行完全表扫描,然后拿源表和目标表检查,匹配条件,若成立则执行SQL语句1,不成立则执行SQL语句2,最执行SQL语句3。
要注意的是:

  1. Merge操作的只是“操作表”,源表不会有任何变化
  2. Merge的最后结尾必须是以分号结束的,不能忘了分号
  3. 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格
  4. 不一定要把三个操作都写全,可以根据实际情况
    1. MERGE
    2. [ TOP ( expression ) [ PERCENT ] ]
    3. [ INTO ] <操作表> --即将做插入、更新、删除的表
    4. USING <源表或者数据集或者子查询> --用户提供匹配条件来源的集合或者表
    5. ON <匹配条件> --可以是任意有效的条件组合
    6. [ WHEN MATCHED [ AND <clause_search_condition> ]--匹配条件成立
    7. THEN <SQL语句1> ]
    8. [ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]--匹配条件不成立
    9. THEN <SQL语句2> ]
    10. [ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]--目标变不存在而源表存在的数据
    11. THEN <SQL语句3> ]
    12. ;--不要忘记分号

    Merge 简单应用:

    1、以另一个表作为源表:

    ```sql —-创建源表 Create Table OriginTable(id Int,caption VarChar(50)) —-创建操作表 Create Table TargetTable(id Int,caption VarChar(50)) —插入测试数据 Insert Into dbo.OriginTable(id,caption)VALUES(1,’测试1’) Insert Into dbo.OriginTable(id,caption)VALUES(2,’测试2’) Insert Into dbo.OriginTable(id,caption)VALUES(3,’测试3’) Insert Into dbo.OriginTable(id,caption)VALUES(4,’测试4’) Insert Into dbo.TargetTable(id,caption)VALUES(1,’目标表匹配到了源表则update1’) Insert Into dbo.TargetTable(id,caption)VALUES(3,’目标表匹配到了源表则update2’) Insert Into dbo.TargetTable(id,caption)VALUES(5,’源表里不存在则delete’) Insert Into dbo.TargetTable(id,caption)VALUES(8,’源表里不存在则delete’) Select from OriginTable Select from TargetTable

MERGE INTO TargetTable as T USING OriginTable as S ON T.id=S.id WHEN MATCHED —当上面on后的T.id=S.id时,则更新,也可以加上自定义的限制条件 MATCHED AND S.id=2 Then UpDate set T.caption=S.caption When Not Matched —目标中没有的id ,在源表中有则插入 Then Insert Values(S.id,S.caption) When Not Matched By SOURCE —目标表中存在源表中不存在则删除 Then Delete;—Merge的最后结尾必须是以分号结束的,不能忘了分号 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格

  1. 原始数据:<br />![](https://cdn.nlark.com/yuque/0/2020/jpeg/278385/1596507140655-0eded943-74dc-40e0-8d0d-7acaba92ea33.jpeg#align=left&display=inline&height=237&margin=%5Bobject%20Object%5D&originHeight=237&originWidth=326&size=0&status=done&style=none&width=326)<br />处理后的TargetTable数据:<br />![](https://cdn.nlark.com/yuque/0/2020/jpeg/278385/1596507141470-be7c5560-7a1b-4d8a-bdac-1805d4efcab6.jpeg#align=left&display=inline&height=137&margin=%5Bobject%20Object%5D&originHeight=137&originWidth=189&size=0&status=done&style=none&width=189)
  2. <a name="37ea1ddb"></a>
  3. ## 2.以子查询作为源表
  4. ```sql
  5. MERGE INTO TargetTable as T
  6. USING (Select top 2 * From TargetTable as a Where a.id<2) as S
  7. ON T.id=S.id
  8. WHEN MATCHED --当上面on后的T.id=S.id时,则更新,也可以加上自定义的限制条件 MATCHED AND S.id=2
  9. Then UpDate set T.caption='8989898'
  10. When Not Matched --目标中没有的id ,在源表中有则插入
  11. Then Insert Values(S.id,'99092')
  12. When Not Matched By SOURCE --目标表中存在源表中不存在则删除
  13. Then Delete;--Merge的最后结尾必须是以分号结束的,不能忘了分号 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格• 1

原始TargetTable数据
MSSQL Merge [Into](一键式更新、插入、删除) - 图1
处理后的targetTable数据:
MSSQL Merge [Into](一键式更新、插入、删除) - 图2

附Oracle 9i以上Merge的语法:

  1. MERGE INTO [your table-name] [rename your table here]
  2. USING ( [write your query here] )[rename your query-sql and using just like a table]
  3. ON ([conditional expression here] AND [...]...)
  4. WHEN MATHED THEN [here you can execute some update sql or something else ]
  5. WHEN NOT MATHED THEN [execute something else here ! ]