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 ...
随机推荐
- jquery live hover事件的替代写法
HTML中的hover行为通常在样式中定义,利用jquery实现此效果有两种情况. 第一种是常见的针对页面中静态的元素,以改变元素样式中的border-color为例,写法如下: $(function ...
- lucene查询排序结果原理总结
参考文章 Lucene3.0结果排序原理+操作+示例 Lucene的排序算法 一句话总结lucene排序算法是什么样的 关键几个概念 参考文档: http://lucene.apache.org/co ...
- maven自动部署到tomcat的问题
最近需要使用Maven将项目自动部署到Tomcat,在网络上也查找了很多文章,内容大同小异,今天打算在这里给自己做一个小总结 参考网址:http://blog.csdn.net/dilaomimi/a ...
- 程序里面的system.out.println()输出到其他位置,不输出到tomcat控制台。
设置startup.bat: call "%EXECUTABLE%" run %CMD_LINE_ARGS% >> ..\logs\kongzitai.txt 将sys ...
- Swift - 20 - 字典的基础操作
//: Playground - noun: a place where people can play import UIKit var dict = [1:"one", 2:& ...
- Scene is unreachable due to lack of entry points and does not have an identifier for runtime access
使用Storyboard时出现以下警告: warning: Unsupported Configuration: Scene is unreachable due to lack of entry p ...
- RedHat9上安装jdk
1.先在windows下载jdk:jdk-6-dlj-linux-i586.bin 2.用ftp上传给linux下 3.chmod 777 jdk-6-dlj-linux-i586.bin 4.将jd ...
- a便签 rel属性改变链接打开页面的方式
<body> XHTML: <a href="http://www.baidu.com" rel="external">Baidu &l ...
- js ie浏览器下的选中操作
最近在学习jquery 好多英文网站,制作一个网站的副本,可以主动地学习.好像给自己的网站添加一个小词典,就像沪江小d那样. js试了好几种方法 实在不行,网上搜索了下 ,用到了浏览器开发.本篇文章 ...
- 基于memcached中命令分析函数tokenize_command改造的split函数
今天使用C重构php代码,需要手写一个split函数,于是就模仿memcached中获取用户命令的函数 static size_t tokenize_command(char *command, to ...