首页 > 其他分享 >MERGE INTO 两个表实现数据同步

MERGE INTO 两个表实现数据同步

时间:2023-03-01 14:45:22浏览次数:29  
标签:同步 dbo INTO ClassName MERGE StudentTotalCount Owner Class

语法:

MERGE INTO 表A USING 表B

ON 关联条件

WHEN MATCHED THEN 满足执行

WHEN NOT MATCHED THEN 不满足执行

  

案例:
merge into [dbo].[T_Class_B]
using [dbo].[T_Class_A] -- 这里的[dbo].[T_Class_A]也可以是子查询
on [T_Class_A].[ClassName]=[T_Class_B].[ClassName]
when matched 
then update  set [T_Class_B].[StudentTotalCount]=[T_Class_A].[StudentTotalCount],[T_Class_B].[Owner]=[T_Class_A].[Owner]
when not matched
then insert([ClassName],[StudentTotalCount],[Owner]) values([T_Class_A].[ClassName],[T_Class_A].[StudentTotalCount],[T_Class_A].[Owner]);

  

结果:

 

 

标签:同步,dbo,INTO,ClassName,MERGE,StudentTotalCount,Owner,Class
From: https://www.cnblogs.com/FanJunCheng521/p/17168104.html

相关文章