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。
要注意的是:
- Merge操作的只是“操作表”,源表不会有任何变化
- Merge的最后结尾必须是以分号结束的,不能忘了分号
- 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格
- 不一定要把三个操作都写全,可以根据实际情况
MERGE
[ TOP ( expression ) [ PERCENT ] ]
[ INTO ] <操作表> --即将做插入、更新、删除的表
USING <源表或者数据集或者子查询> --用户提供匹配条件来源的集合或者表
ON <匹配条件> --可以是任意有效的条件组合
[ WHEN MATCHED [ AND <clause_search_condition> ]--匹配条件成立
THEN <SQL语句1> ]
[ WHEN NOT MATCHED [ BY TARGET ] [ AND <clause_search_condition> ]--匹配条件不成立
THEN <SQL语句2> ]
[ WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]--目标变不存在而源表存在的数据
THEN <SQL语句3> ]
;--不要忘记分号
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的最后结尾必须是以分号结束的,不能忘了分号 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格
原始数据:<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)
<a name="37ea1ddb"></a>
## 2.以子查询作为源表
```sql
MERGE INTO TargetTable as T
USING (Select top 2 * From TargetTable as a Where a.id<2) as S
ON T.id=S.id
WHEN MATCHED --当上面on后的T.id=S.id时,则更新,也可以加上自定义的限制条件 MATCHED AND S.id=2
Then UpDate set T.caption='8989898'
When Not Matched --目标中没有的id ,在源表中有则插入
Then Insert Values(S.id,'99092')
When Not Matched By SOURCE --目标表中存在源表中不存在则删除
Then Delete;--Merge的最后结尾必须是以分号结束的,不能忘了分号 谨记:语法严格要求关键字之间只能有一个英文空格,不能有多余的空格• 1
原始TargetTable数据
处理后的targetTable数据:
附Oracle 9i以上Merge的语法:
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]