‘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的新功能。它可以用来执行插入,更新和删除目标表与源表联接的结果,同时根据操作。嗯,这听起来有点混乱,但让我们来看看如何,它可以帮助我们的一个例子。
假设我们有以下两个表。
Both table are identical in structure (Structure does not need to be identical).
这两个表是相同的结构(结构并不需要是相同的)。
STUDENT_A

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
分享到:
相关推荐
Pro SQL Server 2008 Replication is for Microsoft database developers and administrators who want to learn about the different types of replication and those best suited to SQL Server 2008. This book ...
and grouping sets • Use the TOP option to query and modify data • Use the MERGE statement to modify data based on conditional logic • Maintain and query graph structures with the HIERARCHYID ...
总的来说,优化SQL Server的Merge触发器通常需要对业务逻辑有深入理解,并且要考虑到性能和并发性。使用GROUP BY可以帮助控制触发器的处理方式,避免一次性处理大量行,从而提高系统效率并减少潜在错误。在实际应用...
### SQL Server 2008 R2:关键知识点解析 #### 一、SQL Server 2008 R2概述 **SQL Server 2008 R2** 是Microsoft推出的一款功能强大的关系型数据库管理系统,它在原有的SQL Server 2008基础上进行了全面升级与改进...
《Microsoft SQL Server 2008技术内幕:T-SQL查询》全面深入地介绍了Microsoft SQL Server 2008中高级T-SQL查询、性能优化等方面的内容,以及SQL Server 2008新增加的一些特性。主要内容包括SQL的基础理论、查询优化...
SQL Server 2008的新增内容和更改包括:DATE和TIME数据类型、 hierarchyID数据类型、MERGE命令和多行插入,以及递归查询等。《SQL Server 2008编程入门经典(第3版)》旨在帮助您快速地掌握Microsoft SQL Selwer2008...
《Microsoft SQL Server 2008技术内幕:T-SQL查询》是一本深入探讨SQL Server 2008中T-SQL(Transact-SQL)查询的权威书籍。T-SQL是SQL Server的主要编程语言,用于执行数据操作、管理数据库以及创建复杂的存储过程...
在本课程中,我们将深入探讨SQL Server 2008的T-SQL(Transact-SQL)新特性,这是数据库开发中的重要组成部分。SQL Server 2008作为微软的一款旗舰级数据库管理系统,引入了许多增强功能,提升了开发人员的效率和...
SQL Server 2008的新增内容和更改包括:DATE和TIME数据类型、hierarchyID数据类型、MERGE命令和多行插入,以及递归查询等。《SQL Server 2008编程入门经典(第3版)》旨在帮助您快速地掌握Microsoft SQL Selwer2008的...
《Wrox Professional Microsoft SQL Server 2008 Integration Services》是专为SQL Server 2008的Integration Services(简称SSIS)设计的一本专业指南。这本书深入探讨了这一强大的数据集成和转换工具,帮助读者...
`Merge`函数是SQL Server 2008引入的一个强大的数据操纵语言(DML)关键字,它可以合并`Insert`、`Update`和`Delete`操作到一个单一的语句中,极大地提高了数据库维护的效率和代码的简洁性。在本文中,我们将深入...
Chapter 1, Microsoft SQL Server Database Design Principles, explains the database design process and the architecture and working of the SQL Server 2014 Storage Engine. This chapter covers the ...
using Microsoft.SqlServer.Server; [Serializable] [SqlUserDefinedAggregate( Format.UserDefined, // 使用用户定义的序列化格式 IsInvariantToDuplicates = true, // 允许重复值 IsInvariantToNulls = true, ...
SQL Server 2008 分区表快速创建 SQL Server 2008 分区表快速创建是指在 SQL Server 2008 环境中快速创建分区表的过程。分区表是指将大型表分割成多个小表,以提高查询性能和数据管理效率。下面是快速创建分区表的...
在探讨SQL Server 2008的新特性时,我们聚焦于其对Transact-SQL编程能力的增强,以及对SQL/Common Language Runtime (CLR)的改进。这些新特性旨在提升性能、增加功能,并加强全球化支持,满足了在线事务处理(OLTP)...
在SQL Server 2008之前,这些操作通常需要多个DML语句完成,而MERGE语句则简化了这个过程,提高了代码的可读性和性能。 - **表值参数**:解决了将一组值传递给存储过程或函数的问题。在SQL Server 2005中,通常需要...
SQL Server 2008新增了MERGE语句,用于合并两个数据集,实现插入、更新和删除的综合操作。 3. 数据控制语言(DCL):DCL用于管理数据库用户的权限,包括GRANT、DENY和REVOKE等语句,以控制不同用户对数据库资源的...