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. jqPlot,一个 jQuery这个 JavaScript 框架的绘图插件

    因为项目中需要做报表的功能,于是学习了如何使用jqplot这个绘图插件 结合ajax技术,动态交互后台数据 前前后后花了三四天的时间. 感觉它会出来的想说还可以. 我的后台模板是bootstrap,在 ...

  2. Oracle 用户(user)和模式(schema)的区别

    概述: (一)什么Oracle叫用户(user): A user is a name defined in the database that can connect to and access ob ...

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

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

  4. JSON.parse和JSON.stringify 参数详解

    JSON.parse和JSON.stringify这两个浏览器自带(IE6/7除外)的方法平常我们经常用到,但是一般都只是用到了他们的第一个参数,比如字符串转对象:JSON.parse('{}')   ...

  5. restrict和volatile的作用

    每当看到这两个关键字,我都无比的头痛啊,当时看到理解了一下就明白了,但是在此遇到就忘记是怎么用的了,今天就索性写一写吧,好记性不如烂笔头呗,烂笔头不如存在网上. restrict是c99引入的,关键字 ...

  6. HexColorPicker 让选色变得更简单[for Mac]

    开发iOS的筒子看过来,走过路过,一不小心就错过~ Xcode里的颜色选择器,不能让你随意制定十六进制的颜色,让选色变成了一种折磨,然而作为开发者和设计师又得经常要用到. 现在有了HexColorPi ...

  7. 使用mysql作为hive的元数据库

    1.hive下载安装   2.下载mysql安装   3.以root用户进入mysql命令行:mysql -uroot -p(提示输入密码)   4.创建hive的元数据库:create databa ...

  8. Java简介(1)

    起源 略. 组成 Java由四方面组成: 1.Java编程语言 2.Java文件格式 3.Java虚拟机(JVM) 4.Java应用程序接口(Java api) 体系 JavaSE , JavaEE, ...

  9. javascript写的ajax请求

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  10. iPhone mobile safari fixed 元素滚动慢的问题处理

    最近做一个手机阅读应用,抓取网站数据,做格式化后,适合手机浏览器以及电脑上阅读,不显示任何其他内容无关元素. Site:http://cbread.duapp.com/ 固定左侧边栏时,使用的CSS如 ...