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. jQuery的矿建结构小demo举例

    (function (global) { var document = global.document,//变成局部变量提高搜索的性能 init;// 核心函数 function itcast(sel ...

  2. 解决第三方DLL没有强签名

    -----转载:http://blog.csdn.net/zyming0815/article/details/5939090 创建一个新的随机密钥对:sn -k myTest.snk 第一步: 将D ...

  3. 在Linux-0.11中实现基于内核栈切换的进程切换

    原有的基于TSS的任务切换的不足 进程切换的六段论 1 中断进入内核 2 找到当前进程的PCB和新进程的PCB 3 完成PCB的切换 4 根据PCB完成内核栈的切换 5 切换运行资源LDT 6 利用I ...

  4. 较详细的sqlserver数据库备份、恢复(转)

    C#实现SQL数据库备份与恢复 有两种方法,都是保存为.bak文件.一种是直接用Sql语句执行,另一种是通过引用SQL Server的SQLDMO组件来实现: .通过执行Sql语句来实现 注意,用Sq ...

  5. Uncaught SyntaxError: Unexpected end of input

    js报错  原因:输入的意外终止…… 页面代码写的不规范啊……其中的某条语句,没有正常结束…… 或者部分语句“‘’”双引号,单引号没有配对好,被转义了之类的……错误造成的 代码: <script ...

  6. python -- 函数传参

    一.参数传入规则 可变参数允许传入0个或任意个参数,在函数调用时自动组装成一个tuple: 关键字参数允许传入0个或任意个参数,在函数调用时自动组装成一个dict: 1. 传入可变参数: def ca ...

  7. [C]记录C语言中由于粗心遇到的奇葩错误.

    1. 正确代码: for( word = strtok( buf, whitespace); word != NULL; word = strtok( NULL, whitespace)) { ) 错 ...

  8. linux c数据库备份第四版

    该版本算是比较成熟的啦,欢迎大伙拿来试用!!!1.新增数据库连接和备份时间配置文件conf2.新增日志文件,程序运行的一些异常会记录在log文件下 后续的工作:1.将代码切割为多个文件,分类存放代码2 ...

  9. d037: 鸡兔同笼

    内容: 鸡兔同笼中头的总数,脚的总数,求鸡兔各多少只 输入说明: 一行两个整数分别表示头.脚总数(保证有解,当然有可能解为0) 输出说明: 一行两个整数,分别表示鸡.兔的只数 输入样例:   20 4 ...

  10. soket客户端程序(一)

    soket客户端主要完成以下步骤: 1.建立soket套接字(将套接字理解为一个通道) 2.建立连接 3.向服务器发送http请求 4.接收得到的数据 5.关闭连接 6.本地处理得到的数据 http: ...