1、The use of function merge(update、insert、delete)

Example:

#1.Initialize the data

create table #test(id int ,value int); create table #test2(id int ,value int);

insert into #test values(0,0)

insert into #test values(1,1)

insert into #test values(2,2)

insert into #test values(3,3)

insert into #test2 values(1,11)

insert into #test2 values(2,22)

insert into #test2 values(5,55)

select * from #test

select * from #test2

#2.T-SQL

merge into #test p using (select id,value from  #test2) np on (p.id = np.id)

--Update the record when source id matched target id

when matched then

update set p.name = np.value

--Insert the record when source id not matched target id

when not matched then

insert values (np.id,np.value)

--Delete the records when source id not matched target id and source value >= 2

when not matched by source and value>= 2 then delete;

#3.The result

select * from #test

id value
0 0
1 11
2 22
5 55

The use of function Merge (update、insert、delete)的更多相关文章

  1. SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)

    1.引言 T-SQL(Transact Structured Query Language)是标准的SQL的扩展,是程序和SQL Server沟通的主要语言. T-SQL语言主要由以下几部分组成: 数 ...

  2. mybatis之@Select、@Insert、@Delete、@Param

    之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Map ...

  3. sqlite中的replace、insert、update之前的区别

    本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...

  4. Mybatis select、insert、update、delete 增删改查操作

    MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索.MyBatis 可以使用简单的XML ...

  5. mybatis的select、insert、update、delete语句

    一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...

  6. 转:mybatis——select、insert、update、delete

    一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...

  7. python3 速查参考- python基础 9 -> MySQL基础概念、数据库create、alter、insert、update、delete、select等基础命令

    前置步骤: 下载一个绿色版的mysql数据库客户端连接工具 :http://wosn.net/821.html mysql平台为win7(以后会有CentOS上的) 学习目的: 掌握数据库的基本概念, ...

  8. MySQL中select、insert、update批量操作语句

    项目中经常的需要使用批量操作语句对数据进行操作,使用批量语句的时候需要注意MySQL数据库的条件限制,这里主要是数据的大小限制,这里对批量插入.批量查找及批量更新的基础操作进行下简单的汇总. 1.批量 ...

  9. Android SQLiteDatabase中query、insert、update、delete方法参数说明

    1.SQLiteDataBase对象的query()接口: public Cursor query (String table, String[] columns, String selection, ...

随机推荐

  1. bayes

    from numpy import * import time starttime = time.time() def loadDataSet(): postingList = [['my', 'do ...

  2. 成功启动了Apache却没有启动apache服务器

    原因没有用管理员身份运行...

  3. 限制对比度自适应直方图均衡(Contrast Limited Adaptive histgram equalization/CLAHE)

    转自:http://www.cnblogs.com/Imageshop/archive/2013/04/07/3006334.html 一.自适应直方图均衡化(Adaptive histgram eq ...

  4. Android 之Service

    service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作. 接下来分析一下service 的生命周期: 1: ...

  5. Entity Framewor 学习笔记 (include + where)

    如果我们想在子查询做过滤的话应该怎样写呢? IEnumerable<Product> products = db.products.Include(p => p.colors.Whe ...

  6. 用Setup系列函数完成驱动卸载安装[驱动安装卸载程序]

    // InstallWDFDriver.cpp : Defines the entry point for the console application. // #include "std ...

  7. 【HDOJ】1493 QQpet exploratory park

    超水的动态规划.最后要对概率求Sigma. #include <cstdio> #include <cstring> #include <cstdlib> #def ...

  8. Effective C++笔记(一)——条款26-29

    条款26:尽可能延后变量定义式的出现时间 为何要尽量延后? 当程序中途跳出而导致变量未被使用,但是必须进行构造和析构. 最佳初始化变量 直接在构造时指定初值比构造之后再赋值效率高(条款4) ... s ...

  9. 关于解决Oracle登录:ora-12154:tns:无法解析指定的连接标识符

    (注:此文摘自http://www.linuxidc.com/Linux/2012-04/59322.htm) 开始学习Oracle,所以今天就打算把Oracle 10g安装下.安装完后就来进行测试是 ...

  10. 《SDN核心技术剖析和实战指南》第一章小结

    第一章主要是概况.新技术有一个特点是,每家都有不同的说法.这里我只说说我比较认同的部分. SDN的核心概念大概有两个:转发面与控制面分离.开发可编程化.书里还说逻辑上集中控制,其实这个就可以从转发与控 ...