有这样一个应用:  
 
原始表table1中有个字段qty,sum后大于10000,现在要求不破坏每条记录的值,把table1的记录一条一条抄写到一个结构一样的新表table2中,要求插到table2的sum(qty)>5000时,就停止插入,怎么写这个存储过程,应该要用到游标吧?  

解决方案 »

  1.   

    select *,IDENTITY(int,1,1) as idNew
    into #t from table1insert table2(...)
    select ... from #t a
    where (select sum(qty) from #t where id<=a.id)<=5000drop table #t
      

  2.   

    插到table2的sum(qty)>5000时,就停止插入好像上面少插入了一条select *,IDENTITY(int,1,1) as idNew
    into #t from table1insert table2(...)
    select ... from #t a
    where (select sum(qty) from #t where id<a.id)<=5000drop table #t
      

  3.   

    select *,IDENTITY(int,1,1) as idNew
    into #t from table1delete a from # a
    where (select sum(qty) from #t where id>=a.id)>5000alter table # drop idnew
    goinsert table2 select * from #
    drop table #
      

  4.   

    邹大侠的
    delete a from # a
    where (select sum(qty) from #t where id>=a.id)>5000
    怎么理解,哪位能否解释一下
      

  5.   

    应为: delete a from #t  a
    where (select sum(qty) from #t where id=a.id)>5000
      

  6.   

    where 后面的语句含义能否解释一下?不是语法问题