如何从最后一个端点重新启动游标

时间:2015-12-09 07:48:26

标签: sql sql-server database sql-server-2008-r2 cursor

如何在if条件之后重新启动此游标,并希望row_number在answer.want中再次从1开始,从if条件的结束点重新启动游标。

  declare @SUM DECIMAL(20,2)
  declare @EMPCODE varchar(10)
  declare @NETPAY DECIMAL(10,2)
  declare ABC scroll cursor for 
  select EMPCODE,NETPAY from TXXL where DATE='04-30-2015'
  open ABC
  declare @TEMP table(
  EMPCODE varchar(10) primary key,
  NETPAY DECIMAL(10,2),
  SUM  varchar(max)
   )

  set @SUM=0
  fetch first from ABC into @EMPCODE,@NETPAY

  while @@fetch_status=0

  begin

  fetch next from ABC into @EMPCODE,@NETPAY

  SET @SUM=@SUM+@NETPAY


  if(@SUM<=900000)



  insert into @TEMP(EMPCODE,NETPAY,SUM) values(@EMPCODE,@NETPAY,@SUM)



  end

  select ROW_NUMBER() OVER(ORDER BY EMPCODE) ROWNUMBER,EMPCODE,NETPAY,SUM from @TEMP
close ABC
deallocate ABC

这个光标给出了答案,光标在000179 EMPCODE之后停止工作,因为下一个代码的总和大于900000我想用ROWnumber 1开始下一个代码并且总和那样这个样本的总和: -

ROWNUMBER   EMPCODE NETPAY      SUM
       1    000066  29079.00    29079.00
       2    000076  34515.00    63594.00
       3    000084  15493.00    79087.00
       4    000090  30399.00    109486.00
       5    000109  25812.00    135298.00
       6    000111  31453.00    166751.00
       7    000113  30408.00    197159.00
       8    000116  28040.00    225199.00
       9    000124  27901.00    253100.00
       10   000128  24445.00    277545.00
       11   000131  35760.00    313305.00
       12   000141  23345.00    336650.00
       13   000150  48965.00    385615.00
       14   000179  39350.00    424965.00

想要这样的答案: -

  ROWNUMBER EMPCODE NETPAY      SUM
       1    000066  29079.00    29079.00
       2    000076  34515.00    63594.00
       3    000084  15493.00    79087.00
       4    000090  30399.00    109486.00
       5    000109  25812.00    135298.00
       6    000111  31453.00    166751.00
       7    000113  30408.00    197159.00
       8    000116  28040.00    225199.00
       9    000124  27901.00    253100.00
       10   000128  24445.00    277545.00
       11   000131  35760.00    313305.00
       12   000141  23345.00    336650.00
       13   000150  48965.00    385615.00
       14   000179  39350.00    424965.00
        1   000181  40814.00    40814.00
        2   000182  24335.00    65149.00
        3   000196  29439.00    94588.00
        4   -----    ------      -------
        5   -------- -----      -------

0 个答案:

没有答案