update b set b.列 = a.列 from 表A a left join 表B b on a.主键= b.主键
update 另一个表 set 要更新的列=b.用来更新列 from 另一个表 a join 一个表 b on a.主键列1=b.主键列1 and a.主键列2=b.主键列2
a=要更新的表 b=要更新的列 c=源表 d=源列 update a set a.b=(select d from a inner join c on where a.主键=c.主键) from a,c where a.主键=c.主键
/*--例子 有a,b两个表,要求 用b表的value1更新a表的value1 用b表的value2更新a表的value2 a,b表通过id和name关联 --*/--测试的表及数据 declare @a table(id int,name varchar(10),value1 int,value2 int) insert into @a select 1,'a',1,11 union all select 2,'a',2,22 union all select 1,'a',3,33 union all select 2,'b',4,44 union all select 3,'a',5,55declare @b table(id int,name varchar(10),value1 int,value2 int) insert into @b select 1,'a',11,333 union all select 2,'a',22,222 union all select 3,'a',33,111--更新处理 update @a set value1=b.value1,value2=b.value2 from @a a join @b b on a.id=b.id and a.name=b.name--显示结果 select * from @a/*--测试结果 id name value1 value2 ----------- ---------- ----------- ----------- 1 a 11 333 2 a 22 222 1 a 11 333 2 b 4 44 3 a 33 111(所影响的行数为 5 行) --*/
from 表A a
left join 表B b on a.主键= b.主键
from 另一个表 a join 一个表 b on a.主键列1=b.主键列1 and a.主键列2=b.主键列2
b=要更新的列
c=源表
d=源列
update a set a.b=(select d from a inner join c
on
where a.主键=c.主键)
from a,c
where a.主键=c.主键
有a,b两个表,要求
用b表的value1更新a表的value1
用b表的value2更新a表的value2
a,b表通过id和name关联
--*/--测试的表及数据
declare @a table(id int,name varchar(10),value1 int,value2 int)
insert into @a
select 1,'a',1,11
union all select 2,'a',2,22
union all select 1,'a',3,33
union all select 2,'b',4,44
union all select 3,'a',5,55declare @b table(id int,name varchar(10),value1 int,value2 int)
insert into @b
select 1,'a',11,333
union all select 2,'a',22,222
union all select 3,'a',33,111--更新处理
update @a set value1=b.value1,value2=b.value2
from @a a join @b b on a.id=b.id and a.name=b.name--显示结果
select * from @a/*--测试结果
id name value1 value2
----------- ---------- ----------- -----------
1 a 11 333
2 a 22 222
1 a 11 333
2 b 4 44
3 a 33 111(所影响的行数为 5 行)
--*/