27,579
社区成员
发帖
与我相关
我的任务
分享
--假設你用於更新的數據為表ta,要更新的表為tb
update b set 字段1=a.某字段1,字段2=a.某字段2 from tb b inner join ta a on b.学号=a.学号 --更新存在的
insert tb(字段列表)select 字段列表 from ta where 学号 not in (select 学号 from tb) --插入不存在的
if exists(select 1 from tb where stuno = @stuno) --存在此数据,更新
update .
else --不存在,插入
insert .
if exists(select 1 from tb where stuno = @stuno) --存在此数据,更新
update .
else --不存在,插入
insert .
update a set a.f1=b.f1,a.f2=b.f2 ,... from ta a left join tb b on a.学号 =b.学号
insert ta(f1,f2,...fn) select a.f1,a.f2,...a.fn from tb a
where a.学号 not in (select 学号 from ta)
update a set a.f1=b.f1,a.f2=b.f2 ,... from ta a inner join tb b on a.学号 =b.学号
insert ta(f1,f2,...fn) select a.f1,a.f2,...a.fn from tb a
left join ta b
on a.学号=b.学号
where b.学号 is null