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


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

    1. ------------------------------------------------------------------
    2. --备份履历表【T_NewBarcodeState】到备份表【T_NewBarcodeState_Bak】
    3. ------------------------------------------------------------------
    4. --CREATE TABLE [dbo].[T_NewBarcodeState_Bak](
    5. -- [ID] [varchar](50),
    6. -- [Barcode] [varchar](18),
    7. -- [ProductCode] [varchar](10),
    8. -- [StepNum] [int],
    9. -- [ProductClassify] [nvarchar](20),
    10. -- [ProductType] [nvarchar](20),
    11. -- [CurrentPro] [nvarchar](20),
    12. -- [CurrentProStep] [nvarchar](20),
    13. -- [CurrentProStepState] [nvarchar](20),
    14. -- [QualityState] [nvarchar](10),
    15. -- [Location] [nvarchar](2000),
    16. -- [Defect] [nvarchar](2000),
    17. -- [Disposal] [nvarchar](2000),
    18. -- [Remarks] [nvarchar](2000),
    19. -- [UserName] [nvarchar](20),
    20. -- [ComputerName] [nvarchar](200),
    21. -- [AddTime] [datetime],
    22. -- [SortId] [bigint],
    23. -- [UntreateDefect] [varchar](255),
    24. -- [Remark2] [varchar](255),
    25. --)
    26. alter proc Sp_MERGE_NewBarcodeState
    27. as
    28. begin
    29. MERGE INTO dbo.T_NewBarcodeState_Bak AS targetTable
    30. USING dbo.T_NewBarcodeState AS sourceTable
    31. ON targetTable.ID = sourceTable.ID
    32. --当行匹配时执行更新语句
    33. WHEN MATCHED
    34. THEN UPDATE SET
    35. targetTable.Barcode = sourceTable.Barcode,
    36. targetTable.ProductCode = sourceTable.ProductCode,
    37. targetTable.StepNum = sourceTable.StepNum,
    38. targetTable.ProductClassify = sourceTable.ProductClassify,
    39. targetTable.ProductType = sourceTable.ProductType,
    40. targetTable.CurrentPro = sourceTable.CurrentPro,
    41. targetTable.CurrentProStep = sourceTable.CurrentProStep,
    42. targetTable.CurrentProStepState = sourceTable.CurrentProStepState,
    43. targetTable.QualityState = sourceTable.QualityState,
    44. targetTable.Location = sourceTable.Location,
    45. targetTable.Defect = sourceTable.Defect,
    46. targetTable.Disposal = sourceTable.Disposal,
    47. targetTable.Remarks = sourceTable.Remarks,
    48. targetTable.UserName = sourceTable.UserName,
    49. targetTable.ComputerName = sourceTable.ComputerName,
    50. targetTable.AddTime = sourceTable.AddTime,
    51. targetTable.SortId = sourceTable.SortId,
    52. targetTable.UntreateDefect = sourceTable.UntreateDefect,
    53. targetTable.[Remark2] = sourceTable.[Remark2]
    54. --当行不匹配目标表时执行更新语句
    55. WHEN NOT MATCHED BY TARGET
    56. THEN INSERT VALUES (
    57. ID,
    58. Barcode,
    59. ProductCode,
    60. StepNum,
    61. ProductClassify,
    62. ProductType,
    63. CurrentPro,
    64. CurrentProStep,
    65. CurrentProStepState,
    66. QualityState,
    67. Location,
    68. Defect,
    69. Disposal,
    70. Remarks,
    71. UserName,
    72. ComputerName,
    73. AddTime,
    74. SortId,
    75. UntreateDefect,
    76. Remark2
    77. )
    78. --当行不匹配源表时执行删除语句
    79. WHEN NOT MATCHED BY SOURCE
    80. THEN DELETE
    81. ;
    82. end

  • 相关阅读:
    前端框架中的路由(Routing)和前端导航(Front-End Navigation)
    三款Github Copilot的免费替代
    idea+git回退已经push到远端仓库的分支代码到某个历史版本
    [车联网安全自学篇] 四十九. Android安全之IPC机制敏感信息挖掘
    代码随想录一刷打卡——贪心(下篇)
    pytorch hook机制
    sql语句-实体属性有集合怎么批量查询
    OpenShift AI - 部署并使用 LLM 模型
    【InnoDB Cluster】修改已有集群实例名称及成员实例选项
    [附源码]计算机毕业设计JAVA点餐系统
  • 原文地址:https://blog.csdn.net/jindou910101/article/details/134513927