首先我已经写好了一个p_auto_store_bak的存储过程,该存储过程有6个参数,该6个参数的取值来自于表u_batsale_c_bak,此表中有1000条记录,求解怎么通过游标实现自动从表u_batsale_c_bak中的每条记录里取6个对应值赋给存储过程的6个参数,从而执行存储过程??或者还有别的其它方法吗???

解决方案 »

  1.   

    declare cur cursor for
       select p1,p2,p3,p4,p5,p6 from u_batsale_c_bak;
    open cur;declare @p1 datatype,@p2 datatype,@p3 datatype,@p4 datatype,@p5 datatype,@p6 datatype;
    fetch next from cur into @p1,@p2,@p3,@p4,@p5,@p6;while @@fetch_status=0
    begin
        exec p_auto_store_bak @p1,@p2,@p3,@p4,@p5,@p6;
        fetch next from cur into @p1,@p2,@p3,@p4,@p5,@p6;
    endclose cur;
    deallocate cur;
      

  2.   


    declare cursor1 cursor for   
    select col1,col2,col3,col4,col5,col6
    from u_batsale_c_bak --order by ...

    open cursor1  
    fetch next from cursor_now into @col1,@col2,@col3,@col4,@col5,@col6
    while @@fetch_status = 0
    begin
    exec p_auto_store_bak @col1,@col2,@col3,@col4,@col5,@col6
    fetch next from cursor_now into @col1,@col2,@col3,@col4,@col5,@col6
    end

    close cursor1
    deallocate cursor1
      

  3.   

    还能想到的就是去拼接动态执行的SQL字符串。