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. vijos P1375 大整数(高精不熟的一定要做!)

    /* 我尼玛这题不想说啥了 亏了高精写的熟..... 加减乘除max都写了 高精二分 */ #include<iostream> #include<cstdio> #inclu ...

  2. CSS3新增UI样式

    圆角,border-radius: 1-4个数字/1-4个数字,前面是水平,后面是垂直,不给“/”表示水平和垂直一样,举例如下: <head> <meta http-equiv=&q ...

  3. C#错误异常列表

    Exception: 所有异常对象的基类. SystemException:运行时产生的所有错误的基类. IndexOutOfRangeException:当一个数组的下标超出范围时运行时引发. Nu ...

  4. dedecms 使noflag参数及其过滤多个属性的修改方法

    noflag='h' 是代表不包含头条属性的意思,其中flag就是属性, 自定义属性值:头条[h]推荐[c]图片[p]幻灯[f]滚动[s]跳转[j]图文[a]加粗[b]. noflag过滤多个属性的修 ...

  5. Xcode 的正确打开方式——Debugging(转)

    转自CocoaChina http://www.cocoachina.com/ios/20150225/11190.html 程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不 ...

  6. Delphi 动态改变Rzsplitter的Orientation(方向)属性

    效果图: 原先不知道,弄了半天都改不了RzSplitter.Orientation = orHorizontal / orVertical 然后去查该组件的源代码,原来Orientation不是在Rz ...

  7. javascript入门学习笔记

    <button type="button" onclick="alert('Welcome!')">点击这里</button>alert ...

  8. XML约束

    XML约束--能够看懂约束内容,根据约束内容写出符合规则的xml文件. DTD约束 1)导入dtd方式 内部导入 <!DOCTYPE note [ <!ELEMENT note (to,f ...

  9. macbook Android开发环境搭建,真机调试

    买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...

  10. css expression explaination

    http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx 据说已经被弃用的IE css写法,为了修复一些IE8及老版本 ...