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

解决方案 »

  1.   

    tb1和tb2有什么关系?怎么绑定?
      

  2.   

    另外,tb1中的数据如何与tb2中的nameid对应?它们之间貌似木有关系么.
      

  3.   

    t1和t2貌似没关联,如果要对应,加个自增列对应
    下次更新从BB<>1的开始,或者说你加了自增列后就知道更新到哪了,就是第一个BB不等于1的行
      

  4.   

    --是这个意思么,觉得有点牵强create table tb1(AA varchar(10),BB int)
    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
      

  5.   

    tb3表里的nameid有的不能在出现     t1和t2没关联  一个nameid绑定一个AA列