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. 对话框 自定义 IOS风格 包青天

    activity     private void showDialog1() {         message = "您输入的邮箱后缀不是公司邮箱地址\n将导致您的借款审核不通过,请重新 ...

  2. JS的replace()的应用

    替换字符串中的空格 /\s/ig 例如: var pro="ssss  ssss  sss ddd ss" var protext = pro.replace(/\s/ig,&qu ...

  3. 学习CSS一些事(下)

    2.浮动(float) 浮动(float)特点:1.元素会左移.右移,直到触碰到容器为止.    2.设置浮动元素,仍旧处于标准文档流. 3.当元素没有设置宽度值,而设置了浮动属性,元素的宽度随着内容 ...

  4. webservice调用接口,接口返回数组类型

    1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...

  5. Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能

    效果图如下: 实现平台:xp xe2,其中以上功能的实现,核心主要是参考了万一老师的资料,连接:http://www.cnblogs.com/del/archive/2011/05/12/204411 ...

  6. 微信小应用vs progressive-web-apps

    https://developers.google.com/web/progressive-web-apps/

  7. Android单位度量

    px(像素):屏幕上的点. in(英寸):长度单位.mm(毫米):长度单位.pt(磅):1/72英寸.dp(与密度无关的像素):一种基于屏幕密度的抽象单位.在每英寸160点的显示器上,1dp = 1p ...

  8. 002.AngularJs调用Restful实现CRUD

    本节我们主要给大家介绍AngularJs如何调用Restful,实现数据的CRUD. 主要用到的技术: 后端:ASP.NET WebApi + SQLServer2008 前端:AngularJs,B ...

  9. jQuery简单导航示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. jquery 事件绑定(1)

    $(function(){ $("#panel h5.head").bind("click",function(){ $(this).next().show() ...