SQL中Merge的用法
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的用法的更多相关文章
- SQL中merge into用法
从备份表中更新字段到正式表中,使用 UPDATE 批量更新大量的数据,会出现效率低下,有时候甚至卡死的情况,后面通过使用 MERGE INTO 代替 UPDATE 执行批量更新,会提升执行效率. ME ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- sql中binary_checksum(*)的用法
sql中binary_checksum(*)的用法(转) binary_checksum(*)可以用来检查修改过的行. 同一行在update后,该行的binary_checksum(*)就不同. 如 ...
- SQL中Truncate的用法(转)
转自:http://www.studyofnet.com/news/555.html 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所 ...
- sql中 decode() 的用法
sql中 decode() 的用法 SELECT ID,DECODE(inParam,'Param','value1' ,'value2') name FROM yytj2018 如果 inParam ...
- 十、SQL中EXISTS的用法 十三、sql server not exists
十.SQL中EXISTS的用法 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False EXISTS 指定一个子查询,检测 行 的存在. 语法 ...
- SQL中Truncate的用法
SQL中Truncate的用法转自:http://www.studyofnet.com/news/555.html本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE ...
- SQL2008中Merge的用法
在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...
- SQLServer中merge函数用法详解
http://www.jb51.net/article/75302.htm Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Del ...
随机推荐
- 加载MSCOMCTL.OCX错误处理的几个关键
一.工程文件说明,两个版本Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; MSCOMCTL.OCXObject={831FDD16-0C5C- ...
- jquery属性选择器之 attr
在温习jquery手册的时候,注意到这个,坐下记录. attr(name|properties|key,value|fn) 获取匹配的元素集合中的第一个元素的属性的值 或 设置每一个匹配的元素的一个或 ...
- Python Function Note
Python Function Note #汉诺塔问题Python实现 def my_move(n, a, b, c): if n == 1: print(a + ' --> ' + c) el ...
- JTree用法及JTree使用经验总结
import java.awt.Dimension; import java.awt.Color; import javax.swing.JFrame; import javax.swing. ...
- 你真的了解console吗?
对于前端开发者来说,在开发过程中需要监控某些表达式或变量的值的时候,用 debugger 会显得过于笨重,取而代之则是会将值输出到控制台上方便调试.最常用的语句就是console.log(expres ...
- php xcache 配置 使用 (转载)
xcache的使用与配置 一.安装Xcache # wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz # t ...
- jquery 与其他库冲突解决方案
var j = jQuery.noConflict(); j("div p").hide(); // 基于 jQuery 的代码 $("content").st ...
- Bug: freetype/fterrors.h: No such file or directory
Bug描述: 安装PIL过程中出现题目中的错误信息,具体如下:
- python ATM购物程序
需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...
- 探究Android中Listview显示错乱问题
问题 最近在项目中遇到过一个很棘手的问题,就是ListView在滑动后就莫名其妙的显示错乱,网上查阅资料后问题很容易的就解决了,但是对于问题产生的原因仍是一知半解,所以不甘心的我定下心来,狠读源码,终 ...