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官方图像处 ...
随机推荐
- List<Object>转换为JSONArray二
package com.beijxing.TestMain; import java.util.ArrayList; import java.util.Collection; import java. ...
- JavaScript学习(一)—处理事件
一.处理事件(一) 事件(event)是用户在访问页面时执行的操作.提交表单和在图像上移动鼠标就是两种事件.当浏览器探测到一个事件时,比如用鼠标单击或按键,它可以触发与这个事件相关联的JavaScri ...
- 1_UILabel
// // ViewController.swift // 1_UILabel // // Created by Larry on 2016/12/7. // Copyright © 2016年 nf ...
- Python全栈之路目录结构
基础 1.Python全栈之路-----基础篇 2.Python全栈之路---运算符与基本的数据结构 3.Python全栈之路3--set集合--三元运算--深浅拷贝--初识函数 4.Python全栈 ...
- 01 Apache Solr:提升检索体验 为什么是Solr
背景: 最近开发一个大型的仓储管理平台项目,项目的前身是无数个版本的历史悠久的基于CS模式的Windows桌面程序.然后对于每一个客户,我们可能需要为之定制比较个性化的特殊功能.于是,有一个 ...
- ubuntu14.04 安装系统
p { margin-bottom: 0.1in; line-height: 120% } code.cjk { font-family: "Droid Sans Fallback" ...
- Java(多态)动手动脑
1> 请看以下"变态"的类(参看示例ParentChildTest.java) 上述代码的特点是: 子类和父类定义了一模一样的字段和方法 运行以下测试代码 1. 上边的程序运 ...
- ABAP 字符串操作
1).SHIFT:截断字符串 SHIFT {c} [BY {n} PLACES] [{mode}].: 作用:去掉字符串的前n个位置的字符,如果n未指定,默认为1,如果指定的n小于等于0,则字符串不变 ...
- ubuntu新建用户无法登陆
使用sudo adduser 创建用户,不存在无法登陆问题,如果使用useradd创建用户xx,需要在新建home目录下建立用户目录. 同时,需要修改用户目录的属主,命令:chown xx:xx ...
- 学习varnish随笔
Varnish是一款高性能.开源的反向代理服务器和缓存服务器.Varnish使用内存缓存文件来减少响应时间和网络带宽消耗.这个项目是由挪威的一家报纸Verdens Gang的网络分支起始的,其架构设计 ...