表一UserId
Phone
表二UserId
UserName
UserSex
Phone
因为表二中的Phone里边没有存数据,所以我希望把表一中,与表二UserId相同的数据的Phone加入到表二中。不知道我有没有表述清楚?
谢谢大家了!
Phone
表二UserId
UserName
UserSex
Phone
因为表二中的Phone里边没有存数据,所以我希望把表一中,与表二UserId相同的数据的Phone加入到表二中。不知道我有没有表述清楚?
谢谢大家了!
set phone=a.phone
from ta a,tb b
where a.UserId=b.UserId
from ta a , tb b
where a.UserId=b.UserId
insert into T2(UserId,
,UserName
,UserSex
,Phone
)
select (UserId,'',''
Phone
)
以后每次执行insert时
找个字段当pk
if exsits(select * from T2 where PK=@PK)
insert into T2(UserId,
,UserName
,UserSex
,Phone
)
select (UserId,'',''
Phone
)
else
update T2
set phone=T1.phone
from T1 a,T2 b
where a.PK=b.PK
(
userid int,
phone varchar(100)
)
insert 表一
select 1, '10086'
create table 表二
(
userid int,
UserName varchar(100),
UserSex char(1),
phone varchar(100)
)
insert 表二
select 1, '移动', 'F', null union all
select 2, '联通', 'M', null--SQL
--#1.
update b
set phone = a.phone
from 表一 a inner join 表二 b
on a.UseriD = b.userid
--#2.
declare @temp varchar(100)
update b
set phone = case when @temp is null then phone else @temp end,
@temp = (select top(1) phone from 表一 where userid = b.userid)
from 表二 b--RESULT
/*
userid UserName UserSex phone
1 移动 F 10086
2 联通 M NULL
*/
set A.phone = B.phone
from 表1 A,表2 B
where a.userid = B.userid