原题:
http://community.csdn.net/Expert/topic/5243/5243103.xml?temp=.7722895
可能是我表达不清楚吧。答案并不是我想要的。          if @x>= ( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name )
               begin
                 select @sql= @sql + ' ,其它费1=其它1  '
                 set @x=( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name ) --想办法在判断的同时给变量赋值,不然这句是第二次运行了。???
               end
                else
               begin
                 select @sql= @sql + ',其它费1='+rtrim(@x)
               end

解决方案 »

  1.   

    --条件判断时是不能赋值的,下面的语句不就是执行一次了么!
    ALTER PROCEDURE cp_edit_StudInfo_bank2  
             @stud_name nvarchar(20),--客户名称
     @stud_ID nvarchar(20),--客户帐号
             @stud_meoney int,  --[应收/付金额]
             @stud_meoney1 int  --[实际发生额]
    ...
    ...
    ...
      declare @sql varchar(1000), 
              @x int select @x=select 其它2  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name 
     if @stud_meoney1>=@x
      begin
         select @sql= @sql + ' ,其它费2=其它2  '
      end
    else
      begin
        select @sql= @sql + ' ,其它费2= '+@x
      end
      

  2.   

    有好几个if 的,而且是每下一个if 都要用到@x的值,请认真看代码,下面是下一个if 时的代码
    ::::
            if @x>= ( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name )
                   begin
                     select @sql= @sql + ' ,其它费1=其它1  '
                     set @x=( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name ) --想办法在判断的同时给变量赋值,不然这句是第二次运行了。???
                   end
                    else
                   begin
                     select @sql= @sql + ',其它费1='+rtrim(@x)
                   end
    --------------已经不是用@stud_meoney1 来判断了。
      

  3.   

    加多个变量就可以,几乎所有语言都是这么处理的
    declare  @x1 ...
    set @x1=( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name )
                             if @x>= @x1
                   begin
                     select @sql= @sql + ' ,其它费1=其它1  '
                     set @x=@x1
                   end
                    else
                   begin
                     select @sql= @sql + ',其它费1='+rtrim(@x)
                   end
      

  4.   

    這樣也可以吧   if exists( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name  and 其它1<=@x)
                    begin
                             select @sql= @sql + ' ,其它费1=其它1  '
                             set @x=( select 其它1  from excel_sql where 卡号=@stud_ID and 姓名=@stud_name )
                    end
                else
                    begin
                              select @sql= @sql + ',其它费1='+rtrim(@x)
                    end
    因為  exists 是返回  true or false