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))
  
insert into test1
values(1,'wang'),(2,'trieagle')
  
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
 
 
 
转自  http://blog.csdn.net/gongjian0628/article/details/12751761

MSSQL MERGE语法的更多相关文章

  1. MSSQL,MySQL 语法区别

    1 mysql支持enum,和set类型,sql server不支持 2 mysql不支持nchar,nvarchar,ntext类型 3 mysql的递增语句是AUTO_INCREMENT,而mss ...

  2. MERGE语法详解

    merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...

  3. (转)MERGE语法详解

    merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...

  4. oracle的merge语法

    merge into trade.ttradeseat ausing trade.bs_zrt_tradeseat bon (a.L_FUND_ID = b.l_Fund_Id and a.l_bas ...

  5. SQL Merge 语法 单表查询

    --项目中需要用到Merg语法,于是去网上查了资料,发现竟然都是多表查询,问题是我只有一张表,于是我纳闷了,后来我灵机一动,就搞定了!--表名:t_login(登录表)--字段:f_userName( ...

  6. ORACLE中的MERGE语法使用记录

    项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...

  7. merge 语法解析

    merge into 支持sqlserver 2008 和以上的版本 无论是INSERT还是UPDATE,从执行之间上看,MERGE INTO(MERGE)都要比直接INSERT/UPDATE的效率高 ...

  8. MySQL与MSSQL的一些语法差异(持续更新中)

    分号不能少:分号不能少:分号不能少:重要的事情说3遍 Insert或者Update的数据包含反斜杠\的时候需要进行转义\\,例:insert into tablename(id,name) value ...

  9. MYSQL与MSSQL对比学习

    最近在将公司的一个产品里面相关的MSSQL语句修改为可以在MYSQL上执行的语句 l  优点分析: MYSQL短小精悍,容易上手,操作简单,免费供用的.相对其它数据库有特色又实用的语法多一些.SQL怎 ...

随机推荐

  1. JS小总结

    今天看了一下js的部分总结了一下 函数的定义首先是命名函数function funcName函数还有匿名函数 var funcName = function()普通函数和匿名函数在调用上基本没有什么区 ...

  2. Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  3. PHP explode()函数

    源起:将日期格式的字符串拆分成年.月.日,用于组织关系介绍信的特定位置打印.感谢倪同学提供思路 定义和用法 explode()函数把字符串分割为数组 语法 explode(separator,stri ...

  4. C#、js、json Datetime格式总结

    在工作过程中遇到时间格式的数据在C#.js 和 json保存的不同结果,现在总结一下 JavaScript Parser: 1.数字型时间转字符串时间 如var data = "/Date( ...

  5. linux 服务的操作

    启动和停止服务service 命令用于启动及停止某个服务,例如:service camsd stop    停止 camsd 服务service oracled start   启动 oracled ...

  6. 集成ShareSDK,分享成功后QQ和空间回调不执行的可能原因

    AndroidMainifest.xml中的如箭头所示的id与assets下ShareSDK.xml中配置的QQ的AppId一定要相同.

  7. Android技术积累:开发规范

    转载自Keegan小钢 http://keeganlee.me/post/android/20150709 书写规范 1. 编码方式统一用UTF-8. Android Studio默认已是UTF-8, ...

  8. Swiper 中文API手册(share)

    本文分享自 http://www.cnblogs.com/scavengers/p/3760449.html ---------------------------华丽的分割线------------ ...

  9. C#编程语言与面向对象——抽象基类与接口

    在一个类前加“abstract”关键字,此类就成为抽象类. 对应的,在一个方法前加“abstract”关键字,此方法就成为抽象方法. abstract class Fruit //抽象类 { publ ...

  10. TCP SYN扫描学习笔记

    1.TCP SYN包扫描主机状态的原理:tcp协议规定,当目标主机收到一个tcp syn 包时,若目标主机处于开放状态,会返回给源主机一个tcp ack 包(目的端口开放),或者向源主机发送一个tcp ...