update A set [column]=B.[column] from B where B.PK=A.PK
表A(id,colOld) 表B(id,colNew) update 表A set colOld=(select colNew from 表B where id=表A.id)
update A set 字段名=B.字段名 from A inner join B on A.PK=B.PK
使用cursor的方法 --------------------------------------------------------------------- 为什么要用游标?一条SQL语句可以解决的问题不值当。 满足一下楼主的好奇心,使用游标的实现方式; 注意:假设两个字段都为int类型,楼主根据具体的情况来更改即可: declare @pk int,@column intdeclare t_cursor cursor for select [pk],[column] from Bopen t_cursorfetch next from t_cursor into @pk,@columnwhile @@fetch_status=0 begin update A set [column]=@column where [pk]=@pk fetch next from t_cursor into @pk,@column endclose t_cursor deallocate t_cursor
set
[column]=B.[column]
from
B
where
B.PK=A.PK
表B(id,colNew)
update 表A set colOld=(select colNew from 表B where id=表A.id)
---------------------------------------------------------------------
为什么要用游标?一条SQL语句可以解决的问题不值当。
满足一下楼主的好奇心,使用游标的实现方式;
注意:假设两个字段都为int类型,楼主根据具体的情况来更改即可:
declare @pk int,@column intdeclare t_cursor cursor for select [pk],[column] from Bopen t_cursorfetch next from t_cursor into @pk,@columnwhile @@fetch_status=0
begin
update A set [column]=@column where [pk]=@pk
fetch next from t_cursor into @pk,@column
endclose t_cursor
deallocate t_cursor
赫赫