---文章 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使用例子说明的更多相关文章

  1. 基础排序算法之并归排序(Merge Sort)

    并归排序是学习分治法 (Merge Sort) 的好例子.而且它相对于选择,插入,冒泡排序来说,算法性能有一定提升.我首先会描述要解决的问题,并给出一个并归排序的例子.之后是算法的思路以及给出伪代码. ...

  2. c# mybatis net +mysql

    1找到  mybatis.net最好有个例子 http://www.codeproject.com/Articles/894127/WebControls/#_comments  在这里... 这是一 ...

  3. 转: jdbc连接数据库需要注意和出错的地方

    * 1.数据库登录模式不能只使用windows登录模式,要采取混合模式登录, * 并记住相应的密码和账户: * 2.连接数据库后一定要记得关闭资源,否则就会造成资源浪费. * 关闭的时候也要注意顺序, ...

  4. SQL Server 性能优化之——T-SQL NOT IN 和 NOT Exists

    这次介绍一下T-SQL中“Not IN” 和“Not Exists”的优化. Not IN 和 Not Exists 命令 : 有些情况下,需要select/update/delete 操作孤立数据. ...

  5. 构建一个真实的应用电子商务SportsStore9

    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(九) 实在不好意思,好久没有更新了,我不想找些客观原因来解释,只想请大家见谅!现在我们继续我们的项目,客户 ...

  6. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  7. MySQL/MariaDB表表达式(3):视图

    视图是表表达式的一种,所以它也是虚拟表.对视图操作的时候会通过语句动态的从表中临时获取数据. 1.创建.修改视图 CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED ...

  8. ServiceStack 多租户的实现方案

    以SqlServer为例子说明ServiceStack实现多租户,在SqlServer中创建4个Database:TMaster.T1,T2,T3,为了安全起见 每个Database不用sa账号,而是 ...

  9. python PIL 图像处理库简介(一)

    1. Introduction     PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处 ...

随机推荐

  1. (转)UIWebView全部API学习

    网上找的收藏一下 http://www.myexception.cn/web/1888974.html 最生僻的API做了下划线以及粗体的标注.百度上查了全是拷贝的同一份代码,而且只有代码没有解释,很 ...

  2. C#,java,C++ 等变量命名规则

    命名规则: 必须以“字母” .“_”或“@”开头,不要以数字开头. 后面可以跟任意“数字”,“字母”,“下划线”. ---注意:自己起的名字尽量避免与系统中的关键字重复.不推荐重新定义相同的变量名. ...

  3. MyBatis怎么防止SQL注入

    SQL注入是一种代码注入技术,用于攻击数据驱动的应用,恶意的SQL语句被插入到执行的实体字段中(例如,为了转储数据库内容给攻击者).[摘自] SQL injection - Wikipedia SQL ...

  4. jQuery(window) 和 jQuery(document)的区别

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用

  5. Oracle 命令行导出表

    exp userid=帐号/密码 tables=(TOWN,YIZHI,一带一路企业) file=C:\Users\casm\Desktop\3tabs.dmp

  6. Python os模块之文件操作

    基本概念:C:\\haoguo.txt 路径: C:\\ 文件名: haoguo 后缀名:.txt 1. 文件名与后缀分离 2. 路径与文件分离 3. 获取当前路径 4. 文件名与后缀合并 5. 路径 ...

  7. JS---如何避免用户在请求时“猛击”

    var isAjax=false;//是否正在执行ajax请求,此处表示不在拿数据 var getInfoByTrainCode=function () { if(isAjax) return;//如 ...

  8. Android之ProgressBar

    今天复习一下以前的知识,补充一下ProgressBar控件 progressBar是进度条组件,通常用于用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性. ...

  9. Android 7.0 UICC 分析(一)

    UICC(Universal Intergrated Circuit Card) 框架 * Following is class diagram for uicc classes: * * UiccC ...

  10. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...