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. vsftpd 访问 权限控制

    vsftpd 重启命令 service vsftpd  start|restart|stop vsftpd 关于权限控制,有两个文件分别设置,都会起作用 /etc/vsftpd/user_list / ...

  2. office2003万能密钥

    保证有效 OFFICE 2003 :  GWH28-DGCMP-P6RC4-6J4MT-3HFDY

  3. NET MVC+EF6+Bootstrap

    开源:ASP.NET MVC+EF6+Bootstrap开发框架   前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿1 ...

  4. 编写 capture filters

    编写 capture filters 如有转载,请在转载前给我提一些建议.谢谢. 百度查不到资料,为无能的百度搜索增加点营养的料. 读 http://www.n-cg.net/CaptureFilte ...

  5. 5分钟教你学会JavaScript正则表达式

    正则表达式在实际开发过程中和技术面试过程中的重要性不言而喻,本文仅仅只是教你如何在几分钟之类学会正则表达式,对于它的原理及运行机制不做介绍. 第一:什么是正则 正则表达式是一种用来描述一定数量文本的模 ...

  6. 设计模式(十二): Flyweight享元模式 -- 结构型模式

    说明: 相对于其它模式,Flyweight模式在PHP实现似乎没有太大的意义,因为PHP的生命周期就在一个请求,请求执行完了,php占用的资源都被释放.我们只是为了学习而简单做了介绍. 1. 概述 面 ...

  7. 【转】Linux中断处理学习笔记

    原文网址:http://www.cnblogs.com/GT_Andy/archive/2011/06/21/2086100.html 1.Linux中断的注册与释放: 在<linux/inte ...

  8. MFC之按键消息(长按处理)

    想要实现长按键的一些控制,查了查可以通过捕获键盘事件,然后处理按键时需要进行的操作.下面简单的实现左右按键界面更新数值加减.  1. 重载PreTranslateMessage(MSG* pMsg)函 ...

  9. [Operating System Labs] 我对Linux0.00中 head.s 的理解和注释

    ?21,#  head.s contains the 32-bit startup code.#  head.s 是32位的启动代码 #  Two L3 task multitasking. The ...

  10. JDBC之ResultSet

    ResultSet is a interface More gnerally,ResultSet is a class involves a table returned by executeQuer ...