create table ta(id int,col1 int, col2 int)
create table tb(id int,col1 int,col2 int)
insert ta select 1,1,1
insert ta select 2,3,1
insert tb select 1,3,3
insert tb select 2,1,5
go
declare @s varchar(8000)
select @s = 'update a set '
select @s = @s + 'a.['+colname+']= b.['+colname+'],'
from (
select cast(b.name as varchar)as colname
from sysobjects a,syscolumns b
where a.id = object_id('ta') and a.id = b.id
and exists(
select cast(b.name as varchar)as colname
from syscolumns where id = object_id('tb') and b.name = name and name <> 'id')
) aa
select * from ta
set @s = left(@s,len(@s) - 1)
EXEC ( @s+' from ta a left join tb b on a.id = b.id')
select * from ta
drop table ta,tb
/*
id col1 col2
----------- ----------- -----------
1 1 1
2 3 1
(所影响的行数为 2 行)
(所影响的行数为 2 行)
id col1 col2
----------- ----------- -----------
1 3 3
2 1 5
(所影响的行数为 2 行)
*/