当前位置: 首页 > news >正文

SQL Server - 使用 Merge 语句实现表数据之间的对比同步

在SQL server (2008以上版本)中当需要将一个表(可能另一个库)中数据同步到另一个表中时,可以考虑使用merge语句。
只需要提供:
1.目标表 (target table)
2.数据源表 (source table)
3.连接条件
4.当行匹配时执行更新语句
5.当行不匹配目标表时执行更新语句
6.当行不匹配源表时执行删除语句

------------------------------------------------------------------
--备份履历表【T_NewBarcodeState】到备份表【T_NewBarcodeState_Bak】
------------------------------------------------------------------
--CREATE TABLE [dbo].[T_NewBarcodeState_Bak](
--	[ID] [varchar](50),
--	[Barcode] [varchar](18),
--	[ProductCode] [varchar](10),
--	[StepNum] [int],
--	[ProductClassify] [nvarchar](20),
--	[ProductType] [nvarchar](20),
--	[CurrentPro] [nvarchar](20),
--	[CurrentProStep] [nvarchar](20),
--	[CurrentProStepState] [nvarchar](20),
--	[QualityState] [nvarchar](10),
--	[Location] [nvarchar](2000),
--	[Defect] [nvarchar](2000),
--	[Disposal] [nvarchar](2000),
--	[Remarks] [nvarchar](2000),
--	[UserName] [nvarchar](20),
--	[ComputerName] [nvarchar](200),
--	[AddTime] [datetime],
--	[SortId] [bigint],
--	[UntreateDefect] [varchar](255),
--	[Remark2] [varchar](255),
--)
alter proc Sp_MERGE_NewBarcodeState
as
beginMERGE INTO dbo.T_NewBarcodeState_Bak AS targetTableUSING dbo.T_NewBarcodeState AS sourceTableON targetTable.ID = sourceTable.ID--当行匹配时执行更新语句WHEN MATCHED THEN UPDATE SET targetTable.Barcode = sourceTable.Barcode,targetTable.ProductCode = sourceTable.ProductCode,targetTable.StepNum = sourceTable.StepNum,targetTable.ProductClassify = sourceTable.ProductClassify,targetTable.ProductType = sourceTable.ProductType,targetTable.CurrentPro = sourceTable.CurrentPro,targetTable.CurrentProStep = sourceTable.CurrentProStep,targetTable.CurrentProStepState = sourceTable.CurrentProStepState,targetTable.QualityState = sourceTable.QualityState,targetTable.Location = sourceTable.Location,targetTable.Defect = sourceTable.Defect,targetTable.Disposal = sourceTable.Disposal,targetTable.Remarks = sourceTable.Remarks,targetTable.UserName = sourceTable.UserName,targetTable.ComputerName = sourceTable.ComputerName,targetTable.AddTime = sourceTable.AddTime,targetTable.SortId = sourceTable.SortId,targetTable.UntreateDefect = sourceTable.UntreateDefect,targetTable.[Remark2] = sourceTable.[Remark2]--当行不匹配目标表时执行更新语句WHEN NOT MATCHED BY TARGETTHEN INSERT VALUES (ID,Barcode,ProductCode,StepNum,ProductClassify,ProductType,CurrentPro,CurrentProStep,CurrentProStepState,QualityState,Location,Defect,Disposal,Remarks,UserName,ComputerName,AddTime,SortId,UntreateDefect,Remark2)--当行不匹配源表时执行删除语句WHEN NOT MATCHED BY SOURCETHEN DELETE;
end

http://www.lryc.cn/news/236836.html

相关文章:

  • 【Web】Flask|Jinja2 SSTI
  • SPDK NVMe-oF target多路功能介绍
  • ADAudit Plus:助力企业安全的权威选择
  • sqli-labs关卡18(基于http头部报错盲注)通关思路
  • uni-app顶部导航栏背景色如何设置,微信小程序返回键设置
  • 基于多种设计模式重构代码(工厂、模板、策略)
  • boomYouth
  • 关于这个“这是B站目前讲的最好的【Transformer实战】教程!“视频的目前可以运行的源代码GPU版本
  • STM32定时器输入捕获测量高电平时间
  • 开源WIFI继电器之硬件电路
  • 远程执行ssh脚本
  • excel导入 Easy Excel
  • html实现图片裁剪处理(附源码)
  • 前端语言报错
  • 详细讲解什么是观察者模式
  • 镭速,克服UDP传输缺点的百倍提速传输软件工具
  • Semi-Supervised Multi-Modal Learning with Balanced Spectral Decomposition
  • 3296:【例50.2】 计算书费《信息学奥赛一本通编程启蒙(C++版)》
  • 统一身份认证平台之SSO建设
  • 【开题报告】基于SpringBoot的膳食营养健康网站的设计与实现
  • 超五类网线和六类网线的相同点和区别
  • Linux--初识和基本的指令(1)
  • 万宾科技智能井盖传感器,提升市政井盖健康
  • transformer学习资料
  • 一起学docker系列之四docker的常用命令--系统操作docker命令及镜像命令
  • MySQL 的执行原理(三)
  • 一道好题——分治
  • 庖丁解牛:NIO核心概念与机制详解 02 _ 缓冲区的细节实现
  • Python itertools模块中的combinations() 函数用法
  • 在线预览excel,luckysheet在vue项目中的使用