有二张表表1
编号 姓名 区域
002 小李 北京
003 小名 北京
007 小宋 上海
123 小红 上海
表2
编号 姓名
002 小李5
007 小宋1
怎么把表2姓名批量更新到表1表1结果要
表1
编号 姓名 区域
002 小李5 北京
003 小名 北京
007 小宋1 上海
123 小红 上海
编号 姓名 区域
002 小李 北京
003 小名 北京
007 小宋 上海
123 小红 上海
表2
编号 姓名
002 小李5
007 小宋1
怎么把表2姓名批量更新到表1表1结果要
表1
编号 姓名 区域
002 小李5 北京
003 小名 北京
007 小宋1 上海
123 小红 上海
-->Author:wufeng4552【水族杰纶】
-->Date :2009-08-19 08:41:50
if not object_id('t1') is null
drop table t1
Go
Create table t1([编号] nvarchar(6),[姓名] nvarchar(6),[区域] nvarchar(2))
Insert t1
select N'002',N'小李',N'北京' union all
select N'003',N'小名',N'北京' union all
select N'007',N'小宋',N'上海' union all
select N'123',N'小红',N'上海'
Go
if not object_id('t2') is null
drop table t2
Go
Create table t2([编号] nvarchar(3),[姓名] nvarchar(3))
Insert t2
select N'002',N'小李5' union all
select N'007',N'小宋1'
Go
update t1 set 姓名=t2.姓名 from t2 where t1.编号=t2.编号
select * from t1
/*
编号 姓名 区域
------ ------ ----
002 小李5 北京
003 小名 北京
007 小宋1 上海
123 小红 上海(4 個資料列受到影響)
*/
where 编号=a.编号
and 区域=a.区域
update t1 set 姓名=t2.姓名 from t2 where t1.编号=t2.编号
表一 a
set
a.姓名=b.姓名
from
表二 b
where
a.编号=b.编号