在更新数据仓库时,经常需要根据源表对Target表进行数据同步,Merge 命令具有数据更新,删除,插入的功能,专门用于数据同步,并将数据的更新输出到表中。在使用Merge命令时,需要注意when not matche子句:

  • when not matched by target :当Target Table不匹配时,数据行不存在于Target Table中,存在于Source Table;
  • when not matched by source:当Source Table不匹配时,数据行不存在于Source Table中,存在于Target Table;
  • 当不指定by子句时,默认值是by target;

1,创建示例数据

use tempdb
go create table dbo.tar
(
id int not null,
name varchar(11) not null
)
go
create table dbo.src
(
id int not null,
name varchar(11) not null
)
go
insert into dbo.tar
values(1,'t1'),(2,'t2'),(3,'t3')
insert into dbo.src(id,name)
values(1,'t1'),(2,'s2'),(4,'s4') create table dbo.dt_merge_output
(
action nvarchar(10) not null,
Deleted_ID int not null,
Deleted_Name nvarchar(11) not null,
Inserted_ID int not null,
Inserted_Name nvarchar(11) not null
)
go

2,同步数据,将源表的数据同步到靶表中

merge into dbo.tar as t
using dbo.src as s
on t.id=s.id --matched表示On子句匹配成功,加上额外的and条件,如果when子句成功,那么更新Targe表中的数据
when matched and t.name<>s.name
then update set t.name=s.name --not matched表示On子句不匹配
--ID不存在于Targe表,存在于Source表,则将行插入到Targe表
when not matched --default by target
then insert (id,name)
values(s.id,s.name) --ID存在于Targe表,而不存在于Source表,那么将行从Target表删除
when not matched by source
then delete;

2,使用output子句,将靶表中更新的数据输出

merge into dbo.tar as t
using dbo.src as s
on t.id=s.id --matched表示On子句匹配成功,加上额外的and条件,如果when子句成功,那么更新Targe表中的数据
when matched and t.name<>s.name
then update set t.name=s.name --not matched表示On子句不匹配
--ID不存在于Targe表,存在于Source表,则将行插入到Targe表
when not matched --default by target
then insert (id,name)
values(s.id,s.name) --ID存在于Targe表,而不存在于Source表,那么将行从Target表删除
when not matched by source
then delete
output $action,deleted.id as Deleted_ID,deleted.name as Deleted_Name,inserted.id as Instered_ID,inserted.name as Instered_Name
;

3,将靶表中更新的数据插入到一个表中有两种方式,一种是output into,一种是使用insert into

第一种方式,使用ouput into方式,将数据插入到staging table中

;merge into dbo.tar as t
using dbo.src as s
on t.id=s.id --matched表示On子句匹配成功,加上额外的and条件,如果when子句成功,那么更新Targe表中的数据
when matched and t.name<>s.name
then update set t.name=s.name --not matched表示On子句不匹配
--ID不存在于Targe表,存在于Source表,则将行插入到Targe表
when not matched --default by target
then insert (id,name)
values(s.id,s.name) --ID存在于Targe表,而不存在于Source表,那么将行从Target表删除
when not matched by source
then delete
output $action,deleted.id as Deleted_ID,deleted.name as Deleted_Name,inserted.id as Instered_ID,inserted.name as Instered_Name
into dbo.dt_merge_output
; select *
from dbo.dt_merge_output

第二种方式,将output子句的输出作为派生表,使用Insert Into子句将数据插入到staging 表中

insert into dbo.dt_merge_output
select *
from
(
merge into dbo.tar as t
using dbo.src as s
on t.id=s.id --matched表示On子句匹配成功,加上额外的and条件,如果when子句成功,那么更新Targe表中的数据
when matched and t.name<>s.name
then update set t.name=s.name --not matched表示On子句不匹配
--ID不存在于Targe表,存在于Source表,则将行插入到Targe表
when not matched --default by target
then insert (id,name)
values(s.id,s.name) --ID存在于Targe表,而不存在于Source表,那么将行从Target表删除
when not matched by source
then delete
output $action,deleted.id as Deleted_ID,deleted.name as Deleted_Name,inserted.id as Instered_ID,inserted.name as Instered_Name
) as p(Action,Deleted_ID,Deleted_Name,Instered_ID,Instered_Name)

4,Output子句

Output子句,用于输出在Target Table中更新的数据,在每个数据行中,有一个特殊的字段,$Action,数据类型是nvarchar(10),能够标识出Merge操作的类型:Insert Delete,Update。

<OUTPUT_CLAUSE> ::=
{
  [ OUTPUT <dml_select_list> INTO { @table_variable | output_table } [ ( column_list ) ] ]
  | [ OUTPUT <dml_select_list> ]
} <dml_select_list> ::= { <column_name> | scalar_expression } [ [AS] column_alias_identifier ][ ,...n ]
<column_name> ::= { DELETED | INSERTED | from_table_name } . { * | column_name }| $action

表Deleted和Inserted 是特殊的两个系统表,由系统创建,并且用户语句只能读取,不能修改,作用域是语句级别,当语句结束时,系统自动回收。DELETED 用于标识被Merge命令删除的数据行,INSERTED 用于标识被Merge命令插入的数据行,如果执行的是Update操作,那么inserted 用于标识更新之后的数据,deleted 用于标识数据行更新之前的数据。

5,在使用Merge命令更新Target表时,同一行数据只能被更新一次

If UPDATE is specified in the <merge_matched> clause, and more than one row of <table_source>matches a row in target_table based on <merge_search_condition>, SQL Server returns an error. The MERGE statement cannot update the same row more than once, or update and delete the same row.

Target表中一个数据行只能被更新一次,SQL Server会报错,错误原因是Source Table的中的多行数据和Target Table中一行数据匹配。

The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.

参考MSDN

MERGE (Transact-SQL)

TSQL Merge 用法的更多相关文章

  1. Merge用法

    Merge用来从一个表中选择一些数据更新或者插入到另一个表中.而最终是用更新还是用插入的方式取决于该语句中的条件. 下面我们简单的举一个例子:   SQL> create table merge ...

  2. Oracle之Merge用法

    Merge用来从一个表中选择一些数据更新或者插入到另一个表中.而最终是用更新还是用插入的方式取决于该语句中的条件. 下面我们简单的举一个例子: SQL)) 表已创建. SQL)) 表已创建. SQL, ...

  3. TSQL Merge On子句和When not matched 语义理解

    Merge 的On子句指定Match condition,When子句指定过滤条件,如果Source Table和Targe Table匹配的上,很好理解:如果匹配不上,必须深入理解不匹配的条件,否则 ...

  4. TSql Top 用法

    第一部分:TSql Top 有两种用法 1,限制查询结果集返回的行数或总行数的百分比. 当将 TOP 与 ORDER BY 子句结合使用时,结果集限制为前 N 个已排序行:否则,以未定义的顺序返回前 ...

  5. TSQL HASHBYTES 用法

    HashBytes 使用Hash 算法,能够产生高质量的Hash值,大幅度提高识别数据相异的准确性,但是HashBytes函数无法提供100%的准确度,如果业务逻辑要求不允许有误差,那么不要使用任何H ...

  6. MERGE 用法

    1.不带输出的SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[InsertShiGongJiao] ), @com ...

  7. oracle merge用法

    动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...

  8. Sql server2008中merge用法

    /// <summary> /// 修改:添加条件: AND roleModule.FuncCode = tvpRoleModule.FuncCode /// </summary&g ...

  9. SQL Server merge用法

    有两个表名:source 表和 target 表,并且要根据 source 表中匹配的值更新 target 表. 有三种情况: source 表有一些 target 表不存在的行.在这种情况下,需要将 ...

随机推荐

  1. java变量

    java有四种变量: 成员变量:类成员,在类体内,但在任何方法或构造器之外. 局部变量:在一个代码块中声明并使用. 参数:方法或构造器的变量. 异常处理参数:和参数类似,只是异常处理的自变量而不是方法 ...

  2. 【转】COM技术内幕(笔记)

    COM技术内幕(笔记) COM--到底是什么?--COM标准的要点介绍,它被设计用来解决什么问题?基本元素的定义--COM术语以及这些术语的含义.使用和处理COM对象--如何创建.使用和销毁COM对象 ...

  3. dataTables-使用详细说明整理

    本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方网站:[http://www.datatables.net/] 二:基本使用:[http://www.guoxk.com/node/jqu ...

  4. arm v5,v6,v7?

    http://blog.csdn.net/woshi_ziyu/article/details/7946862

  5. 解决PHP move_uploaded_file函数移动图片失败

    出现的问题描述:今天在实现一个在用户注册时上传头像图片文件的PHP脚本时,出现了问题:PHP脚本在前面已经确定 浏览器端上传文件没有错误.上传的文件是合法的.上传的文件是图像文件.已经在服务器端生成了 ...

  6. myrocks记录格式分析

    概况 rocksdb作为KV存储引擎,那么myrocks记录最终会以kv的形式存储在rocksdb中.MySQL中的表一般由若干索引组成, 在innodb存储引擎中,每个索引对应一颗B树,而在rock ...

  7. Entity FrameWork 单表对多实体

    一个影片信息Clips表,四个字段:clipId,clipName,fileSize,fileName 方案一: [Table("Clips")] public class Cli ...

  8. Web服务器常用设置

    1.Tomcat浏览目录 找到安装目录下的文件/conf/web.xml,  找到以下配置节,将parame-value设置为true即可 <init-param>             ...

  9. SQL Server 常用分页SQL

    今天无聊和朋友讨论分页,发现网上好多都是错的.网上经常查到的那个Top Not in 或者Max 大部分都不实用,很多都忽略了Order和性能问题.为此上网查了查,顺带把2000和2012版本的也补上 ...

  10. [转] 编译安装GCC

    Linux下编写C/C++程序自然缺不了一个优秀的编译器,Linux下比较常见的自然是GCC了. 2015年GCC也出到了5.2.0版本,对于C++11/14也有了更好的支持了. 所以,今天我们就来说 ...