update tb set gdl=gdl+1
   where (gdl-hsl)/gdl<0.15

解决方案 »

  1.   

    --少了一个begin end
    declare   base   cursor   scroll   dynamic 
    for   select   xh,gdl,hsl   from   test 
    for   update   of   xh,gdl,hsl 
    declare   @x1   float,@x2   float,@x3   float,@x4   float,@x5   float 
    set   @x1=10 
    open   base 
    fetch   next   from   base   into   @x3,@x4,@x5 
    while   @x1=0 
    begin 
    select   @x2=((@x4+1)-@x5)/@x4   from   test   
    if   @x2 <0.15 
    begin 
    set   @x1=@x1-1 
    set   @x4=@x4+1 
    update   test 
    set   gdl=@x4 
    where   current   of   base 
    end 
    else 
    fetch   next   from   base   into   @x3,@x4,@x5 
    end
    close   base 
    deallocate   base 
      
    select   *   from   test 
      

  2.   

    去掉else
    declare   base   cursor   scroll   dynamic 
    for   select   xh,gdl,hsl   from   test 
    for   update   of   xh,gdl,hsl 
    declare   @x1   float,@x2   float,@x3   float,@x4   float,@x5   float 
    set   @x1=10 
    open   base 
    fetch   next   from   base   into   @x3,@x4,@x5 
    while   @x1=0 
    begin 
    select   @x2=((@x4+1)-@x5)*1.0/@x4   from   test   --*1.0
    if   @x2 <0.15 
    begin 
    set   @x1=@x1-1 
    set   @x4=@x4+1 
    update   test 
    set   gdl=@x4 
    where   current   of   base 
    end 
    ---else 这个去掉
    fetch   next   from   base   into   @x3,@x4,@x5 
    end
    close   base 
    deallocate   base 
      
    select   *   from   test 
      

  3.   

    if object_id('test') is not null
       drop table test
    go
    create table test(xh float,gdl float,hsl float)
    insert test
    select 1,      100,     97 union all 
    select 2,      100,     96 union all  
    select 3,      100,     95 
    go
    declare   base   cursor for   select   xh,gdl,hsl   from   test 
    declare   @x1   float,@x2   float,@x3   float,@x4   float,@x5   float 
    set   @x1=10 
    open   base 
    fetch   next   from   base   into   @x3,@x4,@x5 
    while   @x1<>0 
    begin
    select   @x2=((@x4+1)-@x5)/@x4   from   test   
    if   @x2 <0.15 
    begin 
    set   @x1=@x1-1 
    set   @x4=@x4+1 
    update   test 
    set   gdl=@x4 
    end  
    fetch   next   from   base   into   @x3,@x4,@x5 
    end
    close   base 
    deallocate   base   select   *   from   test 这样可以了
      

  4.   

    最难回答这种提问写一段不知所谓的代码,加对不上号的解释,也说不清楚目的是什么set   @x1=10                     --这里 @x1初值是10
    open   base 
    fetch   next   from   base   into   @x3,@x4,@x5 
    while   @x1=0                    -- 这个while能执行吗?
      

  5.   

    我还是把我的想法理出来,大家看一下
    --声明游标base
    declare   base   cursor   scroll   dynamic 
    for   select   xh,gdl,hsl   from   test 
    for   update   of   xh,gdl,hsl 
    --声明变量:x3,x4,x5用来存储xh,gdsl,hsl
    declare   @x1   float,@x2   float,@x3   float,@x4   float,@x5   float 
    --使计数器为10
    set   @x1=10 
    open   base 
    fetch   next   from   base   into   @x3,@x4,@x5 
    --循环使@x1为0
    while   @x1=0 
    --首先计算((@x4+1)-@x5)/@x4的值,然后赋值给@x2
    select   @x2=((@x4+1)-@x5)/@x4  
    --测试,如果@x2小于0.15   
    if   @x2 <0.15 
    begin 
    --使计数器减1
    set   @x1=@x1-1 
    --使@x4加1
    set   @x4=@x4+1 
    --更新gdl,使其值加1
    update   test 
    set   gdl=@x4 
    where   current   of   base 
    end 
    --否则下一条
    else 
    fetch   next   from   base   into   @x3,@x4,@x5 
    close   base 
    deallocate   base   
    select   *   from   test