SQl Server 2008之Merge示例

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

在SQL Server 2008的bookonline中对Merge操作的解释为:

根据与源表联接的结果,对目标表执行插入、更新或删除操作。例如,根据在另一个表中找到的差异在一个表中插入、更新或删除行,可以对两个表进行同步。

此操作用处比较广泛,能很好的简化数据更新的过程。其有三个子句:主键匹配更新操作,目标不存在插入操作,源不存在删除操作。当然三个操作在同一个过程中并非必须全部指定,但至少要指定一个操作。具体的语法比较苦涩。下面以具体的例子进行说明:

有两张结构一致的表:test1,test2。建表语句如下:

create table test1 
(id int,name varchar(20)) 
go 
create table test2 
(id int,name varchar(20)) 
go 
insert into test1(id,name) 
values(1,'boyi55'),(2,'51cto'),(3,'bbs'),(4,'fengjicai'),(5,'alis') 
insert into test2(id,name) 
values(1,'boyi'),(2,'51cto')

1.将test1同步到test2中,没有的数据进行插入,已有数据进行更新

merge   test2 t --要更新的目标表 
using test1 s --源表 
on t.id=s.id --更新条件(即主键) 
when matched --如果主键匹配,更新 
then update set t.name=s.name 
when not matched then insert values(id,name);--目标主未知主键,插入。此语句必须以分号结束

运行以下查询查看更新结果

select a.id,a.name as name_1,b.name as name_2  from test1 as a,test2 as b 
where a.id=b.id

id          name_1               name_2 
----------- -------------------- -------------------- 
1           boyi55               boyi55 
2           51cto                51cto 
3           bbs                  bbs 
4           fengjicai            fengjicai 
5           alis                 alis

2.test1中ID为5的记录删除后,用merge同步更新test2。并显示更新内容。

delete from test1 where id=5

go

merge   test2 t 
using test1 s 
on t.id=s.id 
when matched 
then update set t.name=s.name 
when not matched then insert values(id,name);--此处by target可以省略 
when not matched by source then delete--目标中存在源中没有的记录时删除。此处by source不能省略 
output $action ,inserted.name  as new_name,deleted.name as old_name;--输出更新前后name的变化

$action    new_name             old_name 
---------- -------------------- -------------------- 
UPDATE     boyi55               boyi55 
UPDATE     51cto                51cto 
UPDATE     bbs                  bbs 
UPDATE     fengjicai            fengjicai 
DELETE     NULL                 alis

 

3.我们看到上面存在一个问题就是相同的记录也变更新了。因此可以在matched子句中添加一个条件,当更新的列数值不同时再进行更新。在下面的例子中将新建一个表把更新记录写入到日志表中,并且只更新不同记录。

create table test3(action varchar(20),new_name varchar(20),old_name varchar(20))

update test1 
set name='boyi555' 
where id=1

go 
merge   test2 t 
using test1 s 
on t.id=s.id 
when matched and t.name!=s.name 
then update set t.name=s.name 
when not matched then insert values(id,name) 
when not matched by source then delete 
output $action ,inserted.name,deleted.name into test3(action,new_name,old_name);

select* from test3

action               new_name             old_name 
-------------------- -------------------- -------------------- 
UPDATE               boyi555              boyi55

4.我们只将删除操作记录到日志中。

update test1 
set name='bbs.51cto.com' 
where id=1 
go 
delete from test1 where id=3 
go 
insert into test3 
select action,new_name,old_name from 

merge   test2 t 
using test1 s 
on t.id=s.id 
when matched and t.name!=s.name 
then update set t.name=s.name 
when not matched then insert values(id,name) 
when not matched by source then delete 
output $action ,inserted.name ,deleted.name) as del(action,new_name,old_name) 
where action='delete'

action               new_name             old_name 
-------------------- -------------------- -------------------- 
DELETE               NULL                 bbs

5.如果数据量比较大,同步操作分两次完成,每次只更新50%的数据。

update test1 
set name=name+'boyi55' 
go

merge top (50) percent  test2 t 
using test1 s 
on t.id=s.id 
when matched and t.name!=s.name 
then update set t.name=s.name 
when not matched then insert values(id,name) 
when not matched by source then delete 
output $action ,inserted.name ,deleted.name;

(3 row(s) affected) 
$action    name                 name 
---------- -------------------- -------------------- 
UPDATE     bbs.51cto.comboyi55  bbs.51cto.com 
UPDATE     51ctoboyi55          51cto

(2 row(s) affected)










本文转自 boyi55 51CTO博客,原文链接:http://blog.51cto.com/boyi55/166851,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
5月前
|
SQL
简单练习Microsoft SQL Server MERGE同步两个表
【10月更文挑战第13天】本文介绍了在Microsoft SQL Server中使用`MERGE`语句同步两个表的步骤。首先创建源表`SourceTable`和目标表`TargetTable`并分别插入数据,然后通过`MERGE`语句根据ID匹配行,实现更新、插入和删除操作,最后验证同步结果。此方法可根据需求调整以适应不同场景。
250 1
|
5月前
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
81 3
|
6月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
7月前
|
SQL 安全 API
PHP代码审计示例(一)——淡然点图标系统SQL注入漏洞审计
PHP代码审计示例(一)——淡然点图标系统SQL注入漏洞审计
153 4
|
8月前
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
197 13
|
8月前
|
SQL
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
118 9
|
8月前
|
SQL 存储 网络安全
关系数据库SQLserver 安装 SQL Server
【7月更文挑战第26天】
100 6
|
7月前
|
SQL 数据处理 数据库
深入解析SQL中的MERGE语句及其重要性
【8月更文挑战第31天】
194 0
|
8月前
|
存储 SQL C++
对比 SQL Server中的VARCHAR(max) 与VARCHAR(n) 数据类型
【7月更文挑战7天】SQL Server 中的 VARCHAR(max) vs VARCHAR(n): - VARCHAR(n) 存储最多 n 个字符(1-8000),适合短文本。 - VARCHAR(max) 可存储约 21 亿个字符,适合大量文本。 - VARCHAR(n) 在处理小数据时性能更好,空间固定。 - VARCHAR(max) 对于大文本更合适,但可能影响性能。 - 选择取决于数据长度预期和业务需求。
621 1
|
7月前
|
SQL 安全 Java
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client
617 0

热门文章

最新文章