--用cursor效率不理想,可参考下面的
update a set a.col1=b.a
 from TAB1 as a
  inner Join  TAB2 as b on a.B = b.B 

解决方案 »

  1.   

    加个问题:
    我在本机上装的是OFFICE2003,所以引用EXCEL的库是11.0的。但是客户那边用的是2000的,所以EXCEL的库是9.0的,那我要是现在把引用改成EXCEL9.0的库,以后要是客户的OFFICE升级了,我的程序的引用要不要做改动?
      

  2.   

    --用相关子查询的话,是不是应该这样!update tab1
    set col1=(select a
              from tab2
              where b=a.b
              )
    from tab1 a
      

  3.   

    程序会自动根据TAB1.B把相应的TAB2.A填入进去:--创建测试环境
    create table TAB1(Col1 varchar(10),A int,B int)
    create table TAB2(Col1 varchar(10),A int,B int)insert TAB1
    select null,1,10 union
    select null,2,11insert TAB2
    select 'A',100,10 union
    select 'B',101,11 --测试
    update TAB1 set COL1 = (select TAB2.A from TAB2 where TAB2.B = TAB1.B)
    --查看
    select * from TAB1--删除测试环境
    drop table TAB1,TAB2--结果
    /*Col1       A           B           
    ---------- ----------- ----------- 
    100        1           10
    101        2           11(所影响的行数为 2 行)
    */
      

  4.   

    update TAB1 set COL1 = (select TAB2.A from TAB2 where TAB2.B = TAB1.B)
    这条语句的结果是COL1这列的值都是一样的,还是程序会自动根据TAB1.B把相应的TAB2.A填入进去?这这个问题的答案呢?
      

  5.   

    --创建测试环境
    create table TAB1(Col1 varchar(10),A int,B int)
    create table TAB2(Col1 varchar(10),A int,B int)insert TAB1
    select null,1,10 union
    select null,2,11 union 
    select null,3,33insert TAB2
    select 'A',100,10 union
    select 'B',101,11 union
    select 'c',120,33--测试
    go
    declare c cursor  for
     select tab2.a,tab2.b from tab2 inner join tab1 on tab1.b=tab2.b
    open c
    declare @tab2_a int
    declare @tab2_b int
    fetch next from c into @tab2_a,@tab2_b
    while @@fetch_status=0
     begin 
      update tab1 set col1=@tab2_a  where tab1.b=@tab2_b
      fetch next from c into @tab2_a,@tab2_b 
     end
    close c
    deallocate c
    --查看
    select * from TAB1
    go
    select * from tab2--删除测试环境
    drop table TAB1,TAB2