MSSQL MERGE语法
Merge的用法Merge可以完成以下功能:1、 两个表之间数据的更新2、 进行进销存更新库存3、 进行表之间数据的复制语法说明:1、 在语句结束后一定要用分号,否则会提示错误。2、 Merge后为目标表,Using后为数据源表3、 如果有两个When matched,则必须使用and来限定第一个子句,一个子句必须制定一个update,另一个必须制定delete4、 When not matched by target,这个子句处理存在于数据源之中,但不存在目标之中的数据行。5、 When not matched等价于When not matched by target6、 When not mathed by source,这个子句处理,存在于目标中,但是不存在数据表之中的数据行一、两个表之间数据的更新create table test1 (col1 int,col2 varchar(100))create table test2 (col3 int,col4 varchar(100)) insert into test1values(1,'wang'),(2,'trieagle')insert into test2(col3)values(1),(2)merge test2using test1on test1.col1=test2.col3when matched then update set col4=col2; select * from test2结果:col3 col41 wang2 trieagle二、进行进销存更新库存Trade表为模拟进出库记录,正数表示入库,负数表示出库create table stock(id int,qty int)create table trade(id int ,qty int)goinsert into stockvalues (1,10), (2,20)insert into tradevalues(1,10),(1,-5),(1,20),(2,10),(2,-30),(3,5) merge stockusing (select id,qty=sum(qty) from trade group by id) Kon stock.id=k.idwhen matched and (stock.qty+k.qty)=0 then deletewhen matched then update set stock.qty=stock.qty+k.qtywhen not matched by target then insert values(k.id,k.qty); select * from stock结果:id qty1 353 5三、进行表之间数据的复制drop table test1drop table test2create table test1 (col1 int,col2 varchar(100))create table test2 (col3 int,col4 varchar(100)) insert into test1values(1,'wang'),(2,'trieagle') merge test2using test1 on test1.col1 =test2.col3when matched and col2!=col4 then update set col4=col2when not matched then insert values(col1,col2)when not matched by source then delete; select* from test2结果:col3 col41 wang2 trieagle 继续:删掉test1中的一行,然后增加一行Delete test1 where col1=1Insert into test1 values(3,'wyq')然后再执行merge test2using test1 on test1.col1 =test2.col3when matched and col2!=col4 then update set col4=col2when not matched then insert values(col1,col2)when not matched by source then delete; 结果:col3 col42 trieagle3 wyqMSSQL MERGE语法的更多相关文章
- MSSQL,MySQL 语法区别
1 mysql支持enum,和set类型,sql server不支持 2 mysql不支持nchar,nvarchar,ntext类型 3 mysql的递增语句是AUTO_INCREMENT,而mss ...
- MERGE语法详解
merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...
- (转)MERGE语法详解
merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...
- oracle的merge语法
merge into trade.ttradeseat ausing trade.bs_zrt_tradeseat bon (a.L_FUND_ID = b.l_Fund_Id and a.l_bas ...
- SQL Merge 语法 单表查询
--项目中需要用到Merg语法,于是去网上查了资料,发现竟然都是多表查询,问题是我只有一张表,于是我纳闷了,后来我灵机一动,就搞定了!--表名:t_login(登录表)--字段:f_userName( ...
- ORACLE中的MERGE语法使用记录
项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...
- merge 语法解析
merge into 支持sqlserver 2008 和以上的版本 无论是INSERT还是UPDATE,从执行之间上看,MERGE INTO(MERGE)都要比直接INSERT/UPDATE的效率高 ...
- MySQL与MSSQL的一些语法差异(持续更新中)
分号不能少:分号不能少:分号不能少:重要的事情说3遍 Insert或者Update的数据包含反斜杠\的时候需要进行转义\\,例:insert into tablename(id,name) value ...
- MYSQL与MSSQL对比学习
最近在将公司的一个产品里面相关的MSSQL语句修改为可以在MYSQL上执行的语句 l 优点分析: MYSQL短小精悍,容易上手,操作简单,免费供用的.相对其它数据库有特色又实用的语法多一些.SQL怎 ...
随机推荐
- php正则,删除字符串中的中英文标点符号
原理很简单,正则查找字符串,然后替换 英文标点符号,正则中有专用的模式来匹配.中文则需要一一列举 代码: <?php $str = "!@#$%^&*(中'文::﹑•中'文中' ...
- 设置ubuntu 下git 的用户名和邮箱
设置ubuntu 下git 的用户名和邮箱 摘自 慢慢修远路,上下求索心http://yanshaozhi.iteye.com/blog/386752 虽然我没看怎么明白 但我用第一总方法就设置好了 ...
- iOS给UIimage添加圆角的两种方式
众所周知,给图片添加圆角有CALayer的cornerRadius, 比如: 最直接的方法: imgView.layer.cornerRadius1=110; imgView.clipsToBou ...
- [Error] ld returned 1 exit status
试试重启你的编译器,不稳定的编译器可能会有这种情况.当然不排除其他原因,若是重启了还不好使,就要看代码哪写错喽!!
- Chrome和IE中使用window.open函数
做前端开发的人员经常回遇到使用windows.open这个函数来打开一个新的网页窗口,使用这个函数的时候有些需要注意的地方,在Chrome和IE下该函数还是有一些细节性的区别. 以下是我在项目中使用的 ...
- Navicat premium工具常用快捷键
Navicat premium是一款数据库管理工具,它可以以单一程式同时连线到MySQL.PostgreSQL. Oracle .SQL Server 及 SQLite 资料库,让管理不同类型的资料库 ...
- win10删除或更改需要SYSTEM或Administrators权限的文件夹
有时候我们需要删除一些系统文件夹或者文件,但是却经常会出现如下提示: 遇到这些情况,我们可以采用如下的解决手段. 一.右键单击需要更改或删除的文件夹,选择属性. 二.在弹出的窗口菜单里,选择“安全”选 ...
- ES6笔记一
遍历数组: 1:传统的 for (var index = 0; index < myArray.length; index++) { console.log(myArray[index]);} ...
- texturepacker打包图片,场景切换时背景图有黑边
在使用TexturePacker打包图片之后,背景图在场景切换(有切换动画)时,明显能看到有黑边,在百度之后解决了. 知乎上边有网友贴出了两种解决方法,我抄过来如下: 第一种: 修改 ccConfig ...
- 安卓开发学习经历2--《第一行代码》coolweather项目SQL语句同一个“陷阱”掉两次 注意转义字符等特殊字符正确书写 关于Id字段自增加体会
今天,在运行<第一行代码>coolweather第二阶段代码,又一次报错,还是神奇地与昨天相似,提示,city_id字段不存在,这里我有两种理解,一种是sql语句出错了,另外一种是没有获取 ...