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 ...
随机推荐
- Centos 安装 docker 和 docker-compose
一.docker安装 1.卸载旧版本 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ d ...
- 405 - 不允许用于访问此页的 HTTP 谓词的处理办法
今天介绍的是针对访问html页面时出现此类错误的处理办法,如果你的问题页面是其他类型,可以参考如下信息: IIS 返回 405 - 不允许用于访问此页的 HTTP 谓词.终极解决办法!!!! 1.为什 ...
- Docker 入门:镜像
主要内容: 什么是镜像 下载镜像 pull 设置下载加速源 查看镜像 上传镜像 push 什么是镜像(image) 镜像是一个文件系统,提供了容器运行时需要用到的文件和参数配置.相当于平时在使用某个软 ...
- [256个管理学理论]002.青蛙效应(Frog Effect)
青蛙效应(Frog Effect) 从一个话题开始: 当下,社会发展突飞猛进,日新月异.在世界经济危机中,我国国民生产总值增长幅度始终在8%以上,引起世人的瞩目. 但,在国内时常也能听到广大投资者对股 ...
- Servlet配置及生命周期
1.设置Ecilipse快捷 file new 2.创建Servlet程序 1). 创建一个 Servlet 接口的实现类. public class HelloServl ...
- 一个基类Person的多个派生类 代码参考
#include <iostream> #include <cstring> using namespace std; class Person { protected: ch ...
- 泛微 e-cology OA 前台SQL注入
poc https://github.com/AdministratorGithub/e-cology-OA-SQL 用法:python elog_sql.py http://target 不存在返回 ...
- PHP获取临时文件的目录路径
PHP获得临时文件的文件目录相对路径,能够 根据tempnam()和sys_get_temp_dir()函数来完成. 下边我们运用简单的代码实例,给大伙儿介绍PHP获得临时文件的文件目录相对路径的方式 ...
- php序列化和反序列化学习
1.什么是序列化 序列化说通俗点就是把一个对象变成可以传输的字符串. 1.举个例子,不知道大家知不知道json格式,这就是一种序列化,有可能就是通过array序列化而来的.而反序列化就是把那串可以传输 ...
- oeong.xyz
兴趣使然而搭的小破站:oeong.xyz