日期:2014-05-18 浏览次数:20622 次
--> 测试数据:[t1]
if object_id('[t1]') is not null
drop table [t1]
create table [t1](
[cn] varchar(3),
[phone] int,
[bm] int,
[address] varchar(6)
)
insert [t1]
select 'abc',111111,22222,'123abc'
--> 测试数据:[t2]
if object_id('[t2]') is not null drop table [t2]
create table [t2](
[cn] varchar(3),
[phone] int,
[bm] int,
[address] varchar(6),
[ckphone] sql_variant,
[ckbm] sql_variant,
[ckaddress] sql_variant
)
insert [t2]
select 'abc',111111,22223,'abc123',null,null,null
update t2
set [ckphone]=t.phone,[ckbm]=t.bm,[ckaddress]=t.[address]
from(
select * from t1 a where not exists(
select 1 from t2 b where a.cn=b.cn and a.phone<>b.phone
))t where t.cn=t2.cn
select * from t2
/*
cn phone bm address ckphone ckbm ckaddress
abc 111111 22223 abc123 111111 22222 123abc
*/