SQL中Merge的用法

Merge的用法

Merge可以完成以下功能:

1、  两个表之间数据的更新

2、  进行进销存更新库存

3、  进行表之间数据的复制

语法说明:

1、  在语句结束后一定要用分号,否则会提示错误。

2、  Merge后为目标表,Using后为数据源表

3、  如果有两个When matched,则必须使用and来限定第一个子句,一个子句必须制定一个update,另一个必须制定delete

4、  When not matched by target,这个子句处理存在于数据源之中,但不存在目标之中的数据行。

5、  When not matched等价于When not matched by target

6、  When not mathed by source,这个子句处理,存在于目标中,但是不存在数据表之中的数据行

一、两个表之间数据的更新

create table test1(col1 int,col2 varchar(100))

create table test2(col3 int,col4 varchar(100))

insert into test1

values(1,'wang'),(2,'trieagle')

insert into test2(col3)

values(1),(2)

merge test2

using test1

on test1.col1=test2.col3

when matched then update set col4=col2;

select * from test2

结果:

col3        col4

1           wang

2           trieagle

二、进行进销存更新库存

Trade表为模拟进出库记录,正数表示入库,负数表示出库

create table stock(id int,qty int)

create table trade(id int ,qty int)

go

insert into stock

values (1,10),(2,20)

insert into trade

values(1,10),(1,-5),(1,20),(2,10),(2,-30),(3,5)

merge stock

using (select id,qty=sum(qty) from trade group by id) K

on stock.id=k.id

when matched and(stock.qty+k.qty)=0 then delete

when matched then update set stock.qty=stock.qty+k.qty

when not matched by target then insert values(k.id,k.qty);

select * from stock

结果:

id          qty

1           35

3           5

三、进行表之间数据的复制

drop table test1

drop table test2

create table test1(col1 int,col2 varchar(100))

create table test2(col3 int,col4 varchar(100))

merge test2

using test1 on test1.col1 =test2.col3

when matched and col2!=col4 then update set col4=col2

when not matched then insert values(col1,col2)

when not matched by source then delete;

select* from test2

结果:

col3        col4

1           wang

2           trieagle

继续:删掉test1中的一行,然后增加一行

Delete test1 where col1=1

Insert into test1 values(3,'wyq')

然后再执行

merge test2

using test1 on test1.col1 =test2.col3

when matched and col2!=col4 then update set col4=col2

when not matched then insert values(col1,col2)

when not matched by source then delete;

结果:

col3        col4

2           trieagle

3           wyq

SQL中Merge的用法的更多相关文章

  1. SQL中merge into用法

    从备份表中更新字段到正式表中,使用 UPDATE 批量更新大量的数据,会出现效率低下,有时候甚至卡死的情况,后面通过使用 MERGE INTO 代替 UPDATE 执行批量更新,会提升执行效率. ME ...

  2. SQL中distinct的用法

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  3. sql中binary_checksum(*)的用法

    sql中binary_checksum(*)的用法(转) binary_checksum(*)可以用来检查修改过的行. 同一行在update后,该行的binary_checksum(*)就不同. 如 ...

  4. SQL中Truncate的用法(转)

    转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...

  5. sql中 decode() 的用法

    sql中 decode() 的用法 SELECT ID,DECODE(inParam,'Param','value1' ,'value2') name FROM yytj2018 如果 inParam ...

  6. 十、SQL中EXISTS的用法 十三、sql server not exists

    十.SQL中EXISTS的用法 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False EXISTS 指定一个子查询,检测 行 的存在. 语法 ...

  7. SQL中Truncate的用法

    SQL中Truncate的用法转自:http://www.studyofnet.com/news/555.html本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE ...

  8. SQL2008中Merge的用法

    在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...

  9. SQLServer中merge函数用法详解

    http://www.jb51.net/article/75302.htm Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Del ...

随机推荐

  1. 【开源java游戏框架libgdx专题】-12-开发工具-图片合成

    TexturePackerGui工具: 1.工具使用: 首先看到texturepacker的界面 界面介绍: New pack:创建项目按钮,单击后输入文件名称,创建文件. Input directo ...

  2. Windows XP CD 函数不正确

    参考这篇文章:http://support.hp.com/cn-zh/document/c00760286 一,在设备管理中查看,如果刻录机名称中含 ROM,则需确认设备是否可写 二,若确定设备可写, ...

  3. jquery live hover事件的替代写法

    HTML中的hover行为通常在样式中定义,利用jquery实现此效果有两种情况. 第一种是常见的针对页面中静态的元素,以改变元素样式中的border-color为例,写法如下: $(function ...

  4. ecshop首页调用指定商品分类下的商品品牌列表

    转之--http://www.16css.com/ecshop/735.html 通过二次开发可以实现ECSHOP首页调用指定分类下的品牌列表. 第一步: 打开根目录下的index.php 在最后面 ...

  5. [转]Delphi中进行延时的4种方法

    1.挂起,不占CPU sleep 2.不挂起,占cpu procedure Delay(msecs:integer); var FirstTickCount:longint; begin FirstT ...

  6. C语言之头文件,static与const关键字

    [前言] 最近几个月在做一个C语言代码重构的项目,过程中也让我对之前在书本上学习到的东西有些补充和巩固,在本博中总结记录下,梳理下零碎的知识点和经验也加深印象,书写是为了更好地思考.平时也都是用印象笔 ...

  7. centos 用户切换

    在系统的/etc/.bash_profile中已经配置了各种环境变量. 用账户a登陆,ldd xxx.so查看一切链接正常. 用账户root登陆,ldd xxx.so查看一切链接正常. 用账户a登陆, ...

  8. EF学习系列

    http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html http://kb.cnblogs.com/zt/ef/#

  9. underscorejs-max学习

    2.15 max 2.15.1 语法: _.max(list, [iteratee], [context]) 2.15.2 说明: 返回list中的最小值. list为集合,数组.对象.字符串或arg ...

  10. fedora20 播放aiv视频

    环境:fedora20 64位 下载个教程是avi的格式,用FEDORA自带的视频播放提示少插件,搜索又没有.  到网上搜索后,参考下面的方式添加软件仓库后,再打开视频播放顺利装好插件.但视频画件一闪 ...