The use of function Merge (update、insert、delete)
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)的更多相关文章
- SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
1.引言 T-SQL(Transact Structured Query Language)是标准的SQL的扩展,是程序和SQL Server沟通的主要语言. T-SQL语言主要由以下几部分组成: 数 ...
- mybatis之@Select、@Insert、@Delete、@Param
之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Map ...
- sqlite中的replace、insert、update之前的区别
本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...
- Mybatis select、insert、update、delete 增删改查操作
MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索.MyBatis 可以使用简单的XML ...
- mybatis的select、insert、update、delete语句
一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...
- 转:mybatis——select、insert、update、delete
一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...
- python3 速查参考- python基础 9 -> MySQL基础概念、数据库create、alter、insert、update、delete、select等基础命令
前置步骤: 下载一个绿色版的mysql数据库客户端连接工具 :http://wosn.net/821.html mysql平台为win7(以后会有CentOS上的) 学习目的: 掌握数据库的基本概念, ...
- MySQL中select、insert、update批量操作语句
项目中经常的需要使用批量操作语句对数据进行操作,使用批量语句的时候需要注意MySQL数据库的条件限制,这里主要是数据的大小限制,这里对批量插入.批量查找及批量更新的基础操作进行下简单的汇总. 1.批量 ...
- Android SQLiteDatabase中query、insert、update、delete方法参数说明
1.SQLiteDataBase对象的query()接口: public Cursor query (String table, String[] columns, String selection, ...
随机推荐
- 《python基础教程》笔记之 其它语句1
print 相关 print可以打印多个表达式,只要将它们用逗号隔开就好,结果中每个参数之间都会插入一个空格,使用+可以避免空格,如 >>> print 'age:',42age: ...
- C语言+ODBC+SQL 连接
第一步:配置ODBC. ①.在控制面板找到ODBC,或者在控制面板上搜索ODBC.如图: ②.点击ODBC的添加按钮,选择SQL Server,这是会出现创建SQL Server的新数据源的对话框,我 ...
- 指定Action、Category调用系统Activity
1.Intent对象详解 Android的应用程序包含三种重要组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来 ...
- PL/SQL 包头和包体
包用于逻辑组合相关的过程和函数,它由包规范和包体两部分组成,包规范用于定义公用的常量 变量,过程和函数,在SQL*PLUS中建立包规范可以使用CREATE PACKAGE命令. 实例如下: CREAT ...
- HDOJ(HDU) 1562 Guess the number(水题,枚举就行)
Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn ...
- spring 学习2
package com.asm; //...省略导入的相关类 public class MessageController implements Controller { public ModelAn ...
- 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子
KnockOutJS学习系列----(一) 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. 很多时候可能是因为事情一多,然后没法静下来心来去写点东西,学点东西. 也很抱歉,突然看到 ...
- JS(二)
对象里面的属性和方法比较多啊,不容易记住,需要多实践: 1.将一串字符串的顺序颠倒,并实现首尾字母大写的两种方法: <!DOCTYPE html> <html lang=" ...
- jquery 让指定导航隐藏
$(function(){ var aLink=$('.nav-content .nav li a'); // 选中所有a var aText=['星网服务','在线搭配','星网学院','推客联盟' ...
- PHPSTORM实用快捷键
alt + F7 find usages 功能,可以很方便的找到函数在哪里调用了 Ctrl + E 可查看最近打开文件或项目 项目名右键选择"Local History | Show His ...