• 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

  • 相关阅读:
    CRC(循环冗余校验码的校验方法)
    HTML基础
    升讯威在线客服系统客服端英文界面的技术实现方法,客户落地巴西圣保罗
    Idea项目爆红
    别看了,这就是你的题呀(二)
    【Stream】
    医疗产品设计的四个主要因素
    深度学习目标检测模型综述
    掌握核心技巧就能创建完美的目录!如何在Word中自动创建目录
    [好题][曼哈顿距离][二分]Taxi 2022杭电多校第3场 1011
  • 原文地址:https://blog.csdn.net/jindou910101/article/details/134513927