日期:2014-05-18  浏览次数:20497 次

求高手解决数据批量修改语句
mssql数据库中有两个表A、B;表A中有列X、Y,表B中有列M、N,X、Y,M、N中都有数据,如何用数据表A中X列数据,整体替换数据表B中M列数据.
求高手帮助解决.

------解决方案--------------------
SQL code

update b
set
m = x
from
a join b on a.x = b.x and a.y = b.y

------解决方案--------------------
SQL code

use DBTest
go
if OBJECT_ID('A') is not null drop table A
go
create table A
(
X int,
Y int
)
go
insert into A
select 1,1 union all
select 2,2
create table B
(
X int,
Y int,
M int,
N int
)
go
insert into B
select 1,1,11,11 union all
select 2,2,22,22

UPDATE B
SET M=A.X
FROM A INNER JOIN B
ON A.X=B.X AND A.Y=B.Y

------解决方案--------------------
SQL code
UPDATE B SET M=(SELECT X FROM A ta WHERE ta.X=tb.X AND ta.Y=tb.Y) FROM B tb