update tb1 set f3=b.f3, f4=b.f4 from tb1 a, tb2 b where a.f1=b.f1 and a.f2=b.f2
insert into tb1 select b.*from tb2 b left join tb1 on a.f1=b.f1 and a.f2=b.f2
where a.f1 IS NULl and a.f2 IS NULL
表ta、tb及查询结果如下图
...全文
1645打赏收藏
以下功能怎样用一条语句实现,不用一个insert与update
有两个表Tb1与Tb2都有字段F1、F2、F3与F4,若tb1中F1、F2与tb2中F1、F2相同时,就用tb2的F3、F4更新tb1的F3、F4; 若tb2中的F1与F2在Tb1中不存在时候,就把这条记录插入到Tb1,一般我们用下面两条语句书写,一条Insert、一条Update; 请问有没有办法只用一条语句实现? update tb1 set f3=b.f3, f4=b.f4 from tb1 a, tb2 b where a.f1=b.f1 and a.f2=b.f2 insert int
merge tb1 a using tb2 b on a.f1=b.f1 and a.f2=b.f2
when matched then update set f3=b.f3, f4=b.f4
when not matched by target then insert(f1,f2,f3,f4) values(b.f1,b.f2,b.f3,b.f4);