Merge在Sqlserver使用例子说明
---文章 MatchInt的方式
Create table SourceTable([ID] int,[Desc] varchar(50));
Create table TargetTable([ID] int,[Desc] varchar(50));
insert into SourceTable([ID],[Desc]) values(1,'Desc1');
insert into SourceTable([ID],[Desc]) values(2,'Desc2');
insert into SourceTable([ID],[Desc]) values(3,'Desc3');
insert into SourceTable([ID],[Desc]) values(4,'Desc4');
insert into TargetTable([ID],[Desc]) values(1,'SourceTable update');
insert into TargetTable([ID],[Desc]) values(2,'SourceTable update');
insert into TargetTable([ID],[Desc]) values(5,'SourceTable Not update');
insert into TargetTable([ID],[Desc]) values(6,'SourceTable Not update');
truncate table SourceTable;
truncate table TargetTable;
select * from TargetTable
select * from SourceTable
-----
/* Update
merge into TargetTable as T
using SourceTable As S on T.[ID]=S.[ID]
when matched
then update set T.[desc]=S.[desc]
when not matched
then insert values(s.[ID],S.[Desc])
when not matched By source
then delete;*/
---更新內容並輸出更新內容
merge into TargetTable as T
using SourceTable As S on T.[ID]=S.[ID]
when matched
then update set T.[desc]=S.[desc]
when not matched
then insert values(s.[ID],S.[Desc])
when not matched By source
then delete
output $action as
[ACTION],
Inserted.[ID] as InsertID,
Inserted.[Desc] as inserdDesc,
Deleted.[ID] as deleteID,
Deleted.[Desc] as DeleteDesc;
----加入條件
merge into TargetTable as T
using SourceTable As S on T.[ID]=S.[ID]
when matched and S.[ID]=3
then update set T.[desc]=S.[desc]
when not matched
then insert values(s.[ID],S.[Desc])
when not matched By source
then delete
output $action as
[ACTION],
Inserted.[ID] as InsertID,
Inserted.[Desc] as inserdDesc,
Deleted.[ID] as deleteID,
Deleted.[Desc] as DeleteDesc;
----只更新前2行
merge into top(2) TargetTable as T
using SourceTable As S on T.[ID]=S.[ID]
when matched and S.[ID]=3
then update set T.[desc]=S.[desc]
when not matched
then insert values(s.[ID],S.[Desc])
when not matched By source
then delete
output $action as
[ACTION],
Inserted.[ID] as InsertID,
Inserted.[Desc] as inserdDesc,
Deleted.[ID] as deleteID,
Deleted.[Desc] as DeleteDesc;
Merge在Sqlserver使用例子说明的更多相关文章
- 基础排序算法之并归排序(Merge Sort)
并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...
- c# mybatis net +mysql
1找到 mybatis.net最好有个例子 http://www.codeproject.com/Articles/894127/WebControls/#_comments 在这里... 这是一 ...
- 转: jdbc连接数据库需要注意和出错的地方
* 1.数据库登录模式不能只使用windows登录模式,要采取混合模式登录, * 并记住相应的密码和账户: * 2.连接数据库后一定要记得关闭资源,否则就会造成资源浪费. * 关闭的时候也要注意顺序, ...
- SQL Server 性能优化之——T-SQL NOT IN 和 NOT Exists
这次介绍一下T-SQL中“Not IN” 和“Not Exists”的优化. Not IN 和 Not Exists 命令 : 有些情况下,需要select/update/delete 操作孤立数据. ...
- 构建一个真实的应用电子商务SportsStore9
使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(九) 实在不好意思,好久没有更新了,我不想找些客观原因来解释,只想请大家见谅!现在我们继续我们的项目,客户 ...
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- MySQL/MariaDB表表达式(3):视图
视图是表表达式的一种,所以它也是虚拟表.对视图操作的时候会通过语句动态的从表中临时获取数据. 1.创建.修改视图 CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED ...
- ServiceStack 多租户的实现方案
以SqlServer为例子说明ServiceStack实现多租户,在SqlServer中创建4个Database:TMaster.T1,T2,T3,为了安全起见 每个Database不用sa账号,而是 ...
- python PIL 图像处理库简介(一)
1. Introduction PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处 ...
随机推荐
- WCF服务二:创建一个简单的WCF服务程序
在本例中,我们将实现一个简单的计算服务,提供基本的加.减.乘.除运算,通过客户端和服务端运行在同一台机器上的不同进程实现. 一.新建WCF服务 1.新建一个空白解决方案,解决方案名称为"WC ...
- AOP (Aspect-OrientedProgramming)面向切面编程
AOP OOP 面向对象编程 适合自上向下,却不适合自左向右 AOP把软件系统分为两个部分:核心关注点和横切关注点.业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点. 横切关注点的一个特 ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- zabbix微信告警实践
首先你得有个企业号!!!具体操作步骤可以参考http://itnihao.blog.51cto.com/1741976/1733245,里面写的很详细就不重复了. 微信公众号注册并配置完成后,还需要让 ...
- iOS强制横屏
由于项目需求,需要整个项目页面都是竖屏,唯独一个折线图页面强制性横屏显示. 网上逛了许多帖子,也看了好多大神的提供的方法,都没能够实现本屌丝想要的效果.没办法自己研究自己搞,借鉴各路大神的思路,最后费 ...
- Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法
Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...
- redis cluster java client jedisCluster spring集成方法
1.使用jedis的原生JedisCluster spring的applicationContext.xml配置redis的连接.连接池.jedisCluster Bean <bean id=& ...
- 【转】oracle 监听静态注册举例解析
网上有很多关于oracle 监听静态注册的文章,但大多都是简单说说,并没有详细的例子,这里,将结合linux as3 下的oracle 10gR2.0.1 举一个具体的例子 1.在 $ORACLE_H ...
- TCP/IP协议工作原理简述
TCP/IP协议工作原理简述 // */ // ]]> TCP/IP协议工作原理简述 Table of Contents 1 概要 2 应用层 3 传输层 4 网络层 5 链路层 1 概要 ...
- web测试
1.验证码在不同浏览器兼容性(兼容测试) 2.登录时间cookie及session