declare @outlet int
declare @outletstate char(2)
declare @do_time smalldatetimedeclare ac cursor for select outlet,outletstate,do_time from #t1 open acfetch next from ac into @outlet,@outletstate, @do_time
WHILE @@FETCH_STATUS = 0
BEGIN
  if convert(int,(@do_time -@start_date))/7=0 
  begin
    update cm_show_state set sp1=@outletstate
    where outlet =@outlet
    return  --已经return了,后边的代码自然是不会执行
  end
  if convert(int,(@do_time -@start_date))/7=1
  begin
    update cm_show_state set sp2=@outletstate
    where outlet=@outlet
    return  --同上
  end
  .....
  fetch next from ac into @outlet,@outletstate, @do_time
END
CLOSE ac
DEALLOCATE ac

解决方案 »

  1.   

    declare @outlet int
    declare @outletstate char(2)
    declare @do_time smalldatetimedeclare ac cursor for select outlet,outletstate,do_time from #t1 open acfetch next from ac into @outlet,@outletstate, @do_time
    WHILE @@FETCH_STATUS = 0
    BEGIN
      if convert(int,(@do_time -@start_date))/7=0 
      begin
        update cm_show_state set sp1=@outletstate
        where outlet =@outlet
        --return
      end
      if convert(int,(@do_time -@start_date))/7=1
      begin
        update cm_show_state set sp2=@outletstate
        where outlet=@outlet
        --return
      end
      .....
      fetch next from ac into @outlet,@outletstate, @do_time
    END
    CLOSE ac
    DEALLOCATE ac
      

  2.   

    谢谢libin_ftsafe,我试试,也就是说我写的游标是没有问题了?/
    可是他是随机取的啊 ,我希望它从第一条开始,不知道该怎么写。
    declare ac cursor for select outlet,outletstate,do_time from #t1 
    --for forward_only
    如果加上for forward_only会显示出错,不知道为什么
      

  3.   

    试过了,现在是为什么只是修改一行啊??
    就是,我的数据@start_date='2005-08-01'
    而do_time   的值有 '2005-08-13'和'2005-08-20'
    也就是 说当取到do_time ='2005-08-13'时应该修改sp2的state,
    do_time ='2005-08-20'时应该修改sp3的state
    可是却没有修改sp2的值,只是修改了sp3的值
    为什么啊??
      

  4.   

    解决随机的问题,你可以再定义游标的时候加各order by。
      

  5.   

    应该修改那条,除了if判断,还有where限制,请楼主仔细检查。
    还有@start_date从哪里来的?
      

  6.   

    在游标中进行更新数据的时候,我觉得最好采用。
        update cm_show_state set sp2=@outletstate
        where current of T_cursor
    因为游标的指针指向了这一行,如果满足条件,那么就可以更新,如果不行,那么指针下移就可以了。
    如果你才用参数的方式,一旦出现重复参数,那么就会涉及到其他的行了!
      

  7.   

    对了,T_cursor是我乱定义的一个游标名