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官方图像处 ...
随机推荐
- 团队编程——web应用之人事管理系统
本次作业为团队作业,团队博客要求如下:1. 介绍团队情况:包括队长.成员.队名.成员照片.队训--.等:2. 介绍团队项目名称.总体任务,各成员任务等:3. 每个队做 一次需求调研(针对团队项目),要 ...
- 走进异步编程的世界 - 开始接触 async/await
[C#] 走进异步编程的世界 - 开始接触 async/await 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async ...
- Python第一模块
一.Python简介 二.Python种类 三.Python环境 windows: 1.需要配置环境变量 2.更新:卸载重装 linux:1.常用命令: 查看默认Python版本 Python -V ...
- [转]Direct3D 11 Tessellation Tutorial
The new hardware tessellation feature available on Direct3D 11 video cards has great potential, but ...
- Appium学习路—脚本篇(启动app)
启动之前的准备 1.脚本执行前,需要先启动appium的server端, 启动server方法: 打开appium客户端,点击右上角的Launch 2.iOS的测试只能在mac本上做 ...
- apache限制同一ip大并发访问及下载
内容来自以下网站整理 http://www.jb51.net/article/58060.htmhttp://www.2cto.com/os/201202/120665.htmlhttp://blog ...
- HDU 4833 Best Financing (DP)
Best Financing Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- pandas中将timestamp转为datetime
参考自:http://stackoverflow.com/questions/35312981/using-pandas-to-datetime-with-timestamps 在pandas Dat ...
- 同时打开两个excel工作窗口
先打开你想要同时打开的两个excel文件,有两个方法可以同时打开两个窗口:首先选取任意一个文件,1. 点击“窗口”菜单==>"重排窗口"==>选择你想同时打开的样式== ...
- js中的一些容易混淆的方法!
数组的一些方法: 1.join()和split()方法 与之相反的是split()方法:用于把一个字符串分割成字符串数组. 注意返回的数组中不包括separator本身: 提示和注释注释:如果把空 ...