SQL Server游标在While循环(光标范围)内跳过IF条件

时间:2017-07-18 10:16:15

标签: sql-server stored-procedures

我有一个存储过程,我正在执行下面的Cursor代码, 这是我第一次使用光标,

declare @Gold_Min int, @Gold_Max int, @Silver_Min int, @Silver_Max int, @Bronze_Min int, @Bronze_Max int, @gold_c int, @silver_c int, @bronze_c int;
set @gold_c = 0;
set @silver_c = 0;
set @bronze_c = 0;
set @Gold_Max = 100;
set @Gold_Min = 75;
set @Silver_Max = 74;
set @Silver_Min = 50;
set @Bronze_Max = 49;
set @Bronze_Min = 25;

DECLARE @ParticipantId int, @Transaction_date date, @Transaction_Time float, @Transaction_completed int, @gold int, @silver int, @bronze int;
DECLARE @MyCursor CURSOR;
BEGIN
    SET @MyCursor = CURSOR DYNAMIC FOR
    select TransactionsCompleted, Gold, Silver, Bronze from #Gamification_Transaction_Per_Date
OPEN @MyCursor 
FETCH NEXT FROM @MyCursor 
INTO @Transaction_completed , @gold , @silver , @bronze;

WHILE @@FETCH_STATUS = 0
BEGIN
  /*
     YOUR ALGORITHM GOES BELOW   
  */
  while(@Transaction_completed > 0)
  begin
    if(@Transaction_completed >= @Gold_Min and ( (@Transaction_completed <= @Gold_Max) or (@Transaction_completed > @Gold_Max) ))
    BEGIN
        if(@Transaction_completed between @Gold_Min and @Gold_Max)
        BEGIN
            set @Transaction_completed = @Gold_Max - @Transaction_completed;
            set @gold_c = @gold_c + 1;
            break;
        END

        ELSE
        BEGIN
            set @Transaction_completed = @Transaction_completed - @Gold_Max;
            set @gold_c = @gold_c + 1;              
            break;
        END
    END
    if(@Transaction_completed >= @Silver_Min and ( (@Transaction_completed <= @Silver_Max) or (@Transaction_completed > @Silver_Max) ))
    BEGIN
        if(@Transaction_completed between @Silver_Min and @Silver_Max)
        BEGIN
            set @Transaction_completed = @Silver_Max - @Transaction_completed;
            set @silver_c = @silver_c + 1;
            break;
        END

        ELSE
        BEGIN
            set @Transaction_completed = @Transaction_completed - @Silver_Max;
            set @silver_c = @silver_c + 1;              
            break;
        END         
    END
    if(@Transaction_completed >= @Bronze_Min and ( (@Transaction_completed <= @Bronze_Max) or (@Transaction_completed > @Bronze_Max) ))
    BEGIN
        if(@Transaction_completed between @Bronze_Min and @Bronze_Max)
        BEGIN
            set @Transaction_completed = @Bronze_Max - @Transaction_completed;
            set @bronze_c = @bronze_c + 1;
            break;
        END

        ELSE
        BEGIN
            set @Transaction_completed = @Transaction_completed - @Bronze_Max;
            set @bronze_c = @bronze_c + 1;              
            break;
        END
    END
  end
     Update #Gamification_Transaction_Per_Date SET Gold = @gold_c WHERE CURRENT OF @MyCursor
     Update #Gamification_Transaction_Per_Date SET Silver = @silver_c WHERE CURRENT OF @MyCursor
     Update #Gamification_Transaction_Per_Date SET Bronze = @bronze_c WHERE CURRENT OF @MyCursor
  /*
     YOUR ALGORITHM GOES ABOVE
  */
  FETCH NEXT FROM @MyCursor 
  INTO @Transaction_completed , @gold , @silver , @bronze;
END; 

CLOSE @MyCursor ;
DEALLOCATE @MyCursor;

END;

我的动机是更新 #Gamification_Transaction_Per_Date < /强>

表结构定义为:The Table

  • 主查询是While循环中的If循环未执行,而当我在循环外打印'HII我在这里'时,它完全正常。

  • 预期输出为:The Output

  • 任何潜在客户都会非常感激

谢谢&amp;问候 shohil Sethia

0 个答案:

没有答案