22,209
社区成员
发帖
与我相关
我的任务
分享
UPDATE B SET IDMain=A.AutoID
FROM A
INNER JOIN B
ON A.BarCoder=B.BarCoder
update B set IDMain=A.AutoID from A inner join B on A.BarCoder=B.BarCoder
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([AutoID] int,[BarCoder] int)
insert [A]
select 1,12345 union all
select 2,56789
if object_id('[B]') is not null drop table [B]
go
create table [B]([IDMain] int,[BarCoder] int)
insert [B]
select null,12345 union all
select null,12345 union all
select null,12345 union all
select null,56789 union all
select null,56789
---更新---
update B
set IDMain=a.AutoID
from A
where a.BarCoder=b.BarCoder
---查询---
select * from B
---结果---
IDMain BarCoder
----------- -----------
1 12345
1 12345
1 12345
2 56789
2 56789
(所影响的行数为 5 行)
update b set idmain=a.autoid
from 表b b left join 表a a on b.barcoder=a.barcoder
update B
set IDMain=a.AutoID
from A
where a.BarCoder=b.BarCoder
update B set IDMain=A.AutoID from A,B where A.BarCoder=B.BarCoder