DML_Data Modification_MERGE
DML_8-Data Modification_MERGE (将Source表合并到Target)
语法:
MERGE INTO 目标表
USING 源表
WHEN MATCHED AND
(Condition) THEN
UPDATE SET
目标表.Field1 = 源表.Field1,
目标表.Field2 = 源表.Field2,
......
WHEN NOT MATCHED
INSERT (Field1, Field2, ...)
VALUES(VALUE1, VALUE2, ...)
WHEN NOT MATCHED BY SOURCE THEN --假使有此逻辑
DELETE... ; --假使需要这么做 (Note: semicolon不能遗漏)
/*
MERGE语句的用法
*/
-----------------------------------------------------------------------------------------
--表1.Customers
use testdb
IF OBJECT_ID('dbo.Customers','U') IS NOT NULL DROP TABLE dbo.Customers;
CREATE TABLE dbo.Customers
(
custid int NOT NULL,
companyname VARCHAR(25) NOT NULL,
phone VARCHAR(20) NOT NULL,
address VARCHAR(50) NOT NULL,
CONSTRAINT PK_Customers PRIMARY KEY(custid)
)
INSERT INTO dbo.Customers(custid,companyname,phone,address)
VALUES (1,'CUST_1','(111) 111-1111','address 1'),
(2,'CUST_2','(222) 222-2222','address 2'),
(3,'CUST_3','(333) 333-3333','address 3'),
(4,'CUST_4','(444) 444-4444','address 4'),
(5,'CUST_5','(555) 555-5555','address 5');
-----------------------------------------------------------------------------------------
--表2.CustomersStage
IF OBJECT_ID('dbo.CustomersStage','U') IS NOT NULL DROP TABLE dbo.CustomersStage;
CREATE TABLE dbo.CustomersStage
(
custid int NOT NULL,
companyname VARCHAR(25) NOT NULL,
phone VARCHAR(20) NOT NULL,
address VARCHAR(50) NOT NULL,
CONSTRAINT PK_CustomersStage PRIMARY KEY(custid)
)
INSERT INTO dbo.CustomersStage(custid,companyname,phone,address)
VALUES (2,'AAAAA','(222) 222-2222','address 2'),
(3,'CUST_3','(333) 333-3333','address 3'),
(5,'BBBBB','CCCCC','DDDDD'),
(6,'cust_6 (new)','(666) 666-6666','address 6'),
(7,'cust_7 (new)','(777) 777-7777','address 7');
-----------------------------------------------------------------------------------------
SELECT * FROM dbo.Customers
SELECT * FROM dbo.CustomersStage
-----------------------------------------------------------------------------------------
--3.Merge语句 without And condition
MERGE INTO dbo.Customers AS TGT
USING dbo.CustomersStage AS SRC
ON TGT.custid = SRC.custid
WHEN MATCHED THEN --此处没有其他条件,即使来源表与目标表完全相同,也要更新目标表
UPDATE SET
TGT.companyname = SRC.companyname,
TGT.phone = SRC.phone,
TGT.address = SRC.address
WHEN NOT MATCHED THEN
INSERT (custid, companyname, phone, address)
VALUES (SRC.custid,SRC.companyname, SRC.phone, SRC.address);
----------------
Note (此处试验是加了分号结束的,只是为了强调一下必须要以分号结束):
Msg 10713, Level 15, State 1, Line 53
A MERGE statement must be terminated by a semi-colon (;).
-----------------------------------------------------------------------------------------
----4.Merge语句 without And condition, WHEN NOT MATCHED BY SOURCE THEN
MERGE INTO dbo.Customers AS TGT
USING dbo.CustomersStage AS SRC
ON TGT.custid = SRC.custid
WHEN MATCHED THEN
UPDATE SET
TGT.companyname = SRC.companyname,
TGT.phone = SRC.phone,
TGT.address = SRC.address
WHEN NOT MATCHED THEN
INSERT (custid, companyname, phone, address)
VALUES (SRC.custid,SRC.companyname, SRC.phone, SRC.address)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
----------------
--在上一次已经合并的基础上,再次进行合并,所以此处显示5行被合并,2行被删除
(7 row(s) affected)
-----------------------------------------------------------------------------------------
--
--5.Merge语句 with And condition
MERGE INTO dbo.Customers AS TGT
USING dbo.CustomersStage AS SRC
ON TGT.custid = SRC.custid
WHEN MATCHED AND --
(TGT.companyname <> SRC.companyname
or TGT.phone <> SRC.phone
or TGT.address <> SRC.address) THEN
UPDATE SET
TGT.companyname = SRC.companyname,
TGT.phone = SRC.phone,
TGT.address = SRC.address
WHEN NOT MATCHED THEN
INSERT (custid, companyname, phone, address)
VALUES (SRC.custid,SRC.companyname, SRC.phone, SRC.address);
----------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------
建立的2个测试表查询的数据:
合并后的数据:
DML_Data Modification_MERGE的更多相关文章
- DML_Data Modification_UPDATE
DML_Data Modification_UPDATE写不进去,不能专注了...... /* */ ------------------------------------------------- ...
- DML_Data Modification_DELETE
DML_Data Modification_Delete删除记录比较简单,但是需要特别注意,一不小心,就变成了 “从删库到跑路“ 就掉的大了 /* Microsoft SQL Server 2008 ...
- DML_Data Modification_INSERT
Data Modification (INSERT.DELETE.UPDATE.MERGE)之INSERT(基础知识,算是20年来第2次学习MSSQL吧,2005年折腾过一段时间的Oracle)INS ...
- DML_Modifying Data Through Table Expressions_UPDATE
DML_Modifying Data Through Table Expressions_UPDATE之前也学习过使用CTE,再来泛泛的学习下,最后将会将一些书籍上或学习到的CTE来个小结 /* Mi ...
随机推荐
- PHP 连接数据库基础操作
<?phpheader('Content-type:text/html;charset=utf-8');//1建立 或者 关闭mysql服务器 @符号用于屏蔽错误信息$link=@mysql ...
- 4 CSS文本属性
CSStext(文本)属性可定义文本外观,比如文本颜色,对齐文本,装饰文本,文本缩进,行间距等 4.1文本颜色 color属性用于定义文本颜色. div { color: red; } 颜色表示方法: ...
- leetcode350之实现求解两数组交集(包含重复元素)
给定两个数组,编写一个函数来计算它们的交集. 说明: 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致. 我们可以不考虑输出结果的顺序 def binarySearch(nums, t ...
- [Axure教程]0005.系统函数与变量介绍
1.变量的种类: [1].全局变量:可以在整个原型的任意位置调用和修改. [2].局部变量:仅作用于某一事件的某一动作内. [3].自定义变量:自行新建的全局变量. 2.Axure函数: [1].特殊 ...
- [计划任务 - dos]制作任务工具
语法 schtasks /create /tn TaskName /tr TaskRun /sc schedule [/mo modifier] [/d day] [/m month[,month.. ...
- NIO 的非阻塞式网络通信
1.阻塞与非阻塞 ① 传统的 IO 流都是阻塞式的.也就是说,当一个线程调用 read() 或 write()时, 该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因 ...
- 设置Mac终端、pip、Anaconda、PyCharm共用一套环境
最近我在用Macbook Pro练习PyTorch的时候,发现明明在终端已经用pip安装了PyTorch,但在pycharm运行时总是报错:No module named torch. 但是我把同样的 ...
- Vue中控制更新的方式
一. 强制更新的实例方法 vm.$forceUpdate() 迫使 Vue 实例重新渲染.注意它仅仅影响实例本身和插入插槽内容的子组件,而不是所有子组件. 在没有留意到数组或对象的变更检测注意事 ...
- 关于vue+element对ie9的兼容el-upload不支持在IE9上传
关于vue+element对ie9的兼容el-upload不支持在IE9上传 https://lian-yue.github.io/vue-upload-component/#/zh-cn/ 解决方案 ...
- java方法句柄-----5.Method Handles in Java
Method Handles in Java 目录 Method Handles in Java 1.介绍 2.什么是MethodHandle 3. Method Handles vs Reflect ...