tb1
AA BB
as 1
qw 1
we 1
`` 1tb2
id nameid
1 100
2 101
3 103
4 104
`` ``tb3
id nameid key
1 100 as
2 102 qw
3 103 we
4用游标把tb2表的nameid绑定tb1表AA 插入tb3表(tb3.nameid=tb2.nameid,tb3.key=tb1.AA)把tb1表的BB列改为2
AA BB
as 1
qw 1
we 1
`` 1tb2
id nameid
1 100
2 101
3 103
4 104
`` ``tb3
id nameid key
1 100 as
2 102 qw
3 103 we
4用游标把tb2表的nameid绑定tb1表AA 插入tb3表(tb3.nameid=tb2.nameid,tb3.key=tb1.AA)把tb1表的BB列改为2
下次更新从BB<>1的开始,或者说你加了自增列后就知道更新到哪了,就是第一个BB不等于1的行
insert into tb1 select 'as',1
insert into tb1 select 'qw',1
insert into tb1 select 'we',1
insert into tb1 select '``',1
create table tb2(id int,nameid int)
insert into tb2 select 1,100
insert into tb2 select 2,101
insert into tb2 select 3,103
insert into tb2 select 4,104
create table tb3(id int,nameid int,[key] varchar(10))
insert into tb3(id,nameid) select 1,100
insert into tb3(id,nameid) select 2,102
insert into tb3(id,nameid) select 3,103
insert into tb3(id,nameid) select 4,104
go
update a set [key]=c.AA from tb3 a inner join tb2 b on a.nameid=b.nameid inner join(
select row_number()over(order by (select 1))rn,* from tb1
)c on b.id=c.rn
update tb1 set BB=2
select * from tb3
select * from tb1
/*
id nameid key
----------- ----------- ----------
1 100 as
2 102 NULL
3 103 we
4 104 ``(4 行受影响)AA BB
---------- -----------
as 2
qw 2
we 2
`` 2(4 行受影响)*/
go
drop table tb1,tb2,tb3