34,873
社区成员
发帖
与我相关
我的任务
分享--> 测试数据: #1
if object_id('tempdb.dbo.#1') is not null drop table #1
create table #1 (ID int,Name varchar(4),Sex varchar(2),other varchar(8))
insert into #1
select 1,'小明','女','山东'
--> 测试数据: #2
if object_id('tempdb.dbo.#2') is not null drop table #2
create table #2 (ID int,Name varchar(4),Sex varchar(2),other varchar(8))
insert into #2
select 1,'小明','','山东济南' union all
select 2,'小王','男','福建'
update #1 set
Sex=case when b.Sex='' then a.Sex else b.Sex end,
other=case when b.other='' then a.other else b.other end
from #1 a join #2 b on a.id=b.id
insert #1 select * from #2 where not exists (select 1 from #1 where ID=#2.ID)
select * from #1
/*
ID Name Sex other
----------- ------ ----- --------
1 小明 女 山东济南
2 小王 男 福建
*/
update 表一
set Sex = isnull((select sex from 表二 where name = 表一.name),表一.sex),
other = isnull((select other from 表二 where name = 表一.name),表一.other)update 表一
set Sex = isnull((select sex from 表二 where name = 表一.name),表一.name),
other = isnull((select other from 表二 where name = 表一.name),表一.other)update Person1 set
Sex=case when b.Sex='' then Sex else b.Sex end,
other=case when b.other='' then other else b.other end
from Person1 a join Person2 b on a.id=b.id