设为首页 收藏本站
查看: 1883|回复: 0

[经验分享] [MSSQL]SQL Server 2008 Using the MERGE statement

[复制链接]

尚未签到

发表于 2016-11-5 05:17:00 | 显示全部楼层 |阅读模式
  ‘MERGE’ statement is a new feature in SQL Server 2008. It can be used to perform insert, update and delete operation on a destination table simultaneously based on the results of a join with a source table. Well, it sounds like a bit confusing, but let's
see an example on how it can help us.
  Assume we have following two tables.
  “合并”的声明是在SQL Server 2008的新功能。它可以用来执行插入,更新和删除目标表与源表联接的结果,同时根据操作。嗯,这听起来有点混乱,但让我们来看看如何,它可以帮助我们的一个例子。

假设我们有以下两个表。


  • STUDENT_A
  • STUDENT_B
  Both table are identical in structure (Structure does not need to be identical).
  这两个表是相同的结构(结构并不需要是相同的)。

STUDENT_A
DSC0000.jpg

STUDENT_B

  And we have to update the ‘STUDENT_A’ with the details at ‘STUDENT_B’. We need to compare and if student ID’s are matched, ‘A’ table should be updated with the ‘B’ table. And if the ID’s in ‘B’ Table are new then we have to insert those to the ‘A’ table.
  和我们有更新的“STUDENT_A与在”STUDENT_B的细节。我们需要比较,如果学生证的匹配,“A”表应与“B”表的更新。如果在“B”表的ID是新的,然后我们要插入的“A”表。

  So using the ‘MERGE’ statement we can achieve this in one execution.因此,使用“合并”的声明,我们可以实现在一个执行
  Syntax:

MERGE  <Target> [AS T]
USING    <Source> [AS S]
ON <Condition>
[WHEN MATCHED THEN <Execution>]
[WHEN NOT MATCHED BY TARGET <Execution>]
[WHEN NOT MATCHED BY SOURCE <Execution>]
  And to do the above operation use the following code:

MERGE STUDENT_A AS T
USING STUDENT_B AS S
ON T.ID = S.ID
WHEN MATCHED THEN UPDATE SET T.AGE = S.AGE
WHEN NOT MATCHED THEN INSERT (ID, FNAME, LNAME, AGE) VALUES(S.ID,S.FNAME,S.LNAME,S.AGE);
  **Please note that semicolon ‘;’ is mandatory.
  So after executing the above code, and if you inspect the Table ‘A’, you can see that it’s updated the way we wanted.

  And also you can use additional rules other than your condition. To illustrate that, first we insert a record to both the tables.

insert into STUDENT_A
select 10, 'John','Doe',30
insert into STUDENT_B
select 10, 'John','Doe',30
  And using the following code you can remove the record with matches the condition and have the value 10.

MERGE STUDENT_A AS T
USING STUDENT_B AS S
ON T.ID = S.ID
WHEN MATCHED and S.ID < 5 THEN UPDATE SET T.AGE = S.AGE
WHEN MATCHED and S.ID = 10 THEN DELETE
WHEN NOT MATCHED BY TARGET THEN INSERT (ID, FNAME, LNAME, AGE)
VALUES(S.ID,S.FNAME,S.LNAME,S.AGE);
  And if you inspect the table A, you can see that it has the same following results:

  
  关于更多内容:http://www.cnblogs.com/CareySon/archive/2012/03/07/2383690.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-295821-1-1.html 上篇帖子: [sql server] 分区视图修改基表的问题 下篇帖子: sql server 判断表/视图/存储过程是否存在
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表