如题,我如果已经打开一个数据集,如select @a=select a from table1 where b =@c,因为我要循环处理,table数据又比较大,所以想把table一次性打开就行了,不要循环一次就得做一次select.在存储过程中该怎么做?

解决方案 »

  1.   

    decalre cursor cur_a for 
     select  a from table1 where ...
    open cur_a 
    fetch next from cur_a into @a
    while @@fetch_status=0
    begin
    ....
    ....
    fetch next from cur_a into @a
    end
    close cur_a
    deallocate cur_a
      

  2.   

    我可能没说清.以baoshan(石头)的例子为例
    decalre @a varchar(50),@b varchar(50)
    decalre cursor cur_a for 
     select  a from table1 where ...
    open cur_a 
    fetch next from cur_a into @a
    while @@fetch_status=0
    begin
      select @b=select b from table2 where b =@a //我是不想每次打开这个select语句.table比较大,如何方便的处理这步?
      insert into table3 (b) values (@b)
    ....
    fetch next from cur_a into @a
    end
    close cur_a
    deallocate cur_a
      

  3.   

    用动态sql,拼接以后一起插入
    或者用临时表处理,先取出部分数据集