【问题标题】:Can I have a CASE statement within a WHILE loop?我可以在 WHILE 循环中使用 CASE 语句吗?
【发布时间】:2011-02-24 08:16:51
【问题描述】:

这就是我正在做的:

while (@counter < 3 and @newBalance >0)
begin   
CASE
when  @counter = 1 then  ( @monFee1 = @monthlyFee, @newBalance = @newBalance-@fee)
when  @counter = 2 then  ( @monFee2 = @monthlyFee, @newBalance = @newBalance-@fee)
END
@counter = @counter +1
end

我收到此错误:

关键字“CASE”附近的语法不正确。

不知道为什么。请帮忙!

【问题讨论】:

    标签: sql case while-loop


    【解决方案1】:

    不,SQL 中的 CASE 结构是为了返回一个值,而不是为了程序流。您需要将其分解为 IF 语句。

    【讨论】:

      【解决方案2】:

      对于你的提议,你应该使用 IF 语句

      While (@counter < 3 and @newBalance >0)
      Begin
          If @Counter = 1 Then
              Begin
                  Set @monFee1 = @monthlyFee
                  Set @newBalance = @newBalance-@fee
              End
      
          If @Counter = 2 Then
              Begin
                  Set @monFee2 = @monthlyFee
                  Set @newBalance = @newBalance-@fee
              End
      
          Set @counter = @counter +1 
      End
      

      【讨论】:

      【解决方案3】:

      CASE 语句与过程代码中的表亲不同,用于分支逻辑。它将在结果集中返回一个结果,这样当您为变量分配值时,您可以确定该值将不是您分配给哪个变量。

      不幸的是,

      (@monFee1 = @counter, @newBalance = @newBalance-@fee)
      

      不返回值。

      另一种使用 If/Else 分支逻辑的方法是

      while (@counter < 3 and @newBalance >0)
      begin   
          IF @counter = 1
          THEN
              SET @monFee1 = @monthlyFee
              SET @newBalance = @newBalance-@fee
          END
          ELSE IF @counter = 2
          BEGIN
              SET @monFee2 = @monthlyFee
              SET @newBalance = @newBalance-@fee
          END
              SET @counter = @counter +1
      end
      

      【讨论】:

        【解决方案4】:

        您不应该在这里使用 while 循环。您实际上只检查了一次 @newBalance 的值。考虑:

        @monFee1 = @monthlyFee
        @newBalance = @newBalance-@fee
        
        IF @newBalance > 0
        BEGIN
           @monFee2 = @monthlyFee
           @newBalance = @newBalance-@fee
        END 
        

        【讨论】:

        • 两个月真的很生气,到12个月会怎样?
        • 然后他写了一个递归函数。
        【解决方案5】:

        由于 CASE 是一个表达式,您可以在 SET 赋值语句中使用它。

        WHILE (@counter < 3 and @newBalance >0) 
        BEGIN
          SET @monFee1 = CASE WHEN @Counter=1
            THEN @monthlyFee ELSE @monFee1 END
          SET @monFee2 = CASE WHEN @Counter=2
            THEN @monthlyFee ELSE @monFee2 END
          SET @newBalance = @newBalance - CASE WHEN @Counter in (1, 2)
            THEN @fee ELSE 0 END
          SET @counter = @counter +1 
        END
        

        它也可以在 SELECT 赋值语句中。

        WHILE (@counter < 3 and @newBalance >0) 
        BEGIN
          SELECT
            @monFee1 = CASE WHEN @Counter=1
              THEN @monthlyFee ELSE @monFee1 END,
            @monFee2 = CASE WHEN @Counter=2
              THEN @monthlyFee ELSE @monFee2 END,
            @newBalance = @newBalance - CASE WHEN @Counter in (1, 2)
              THEN @fee ELSE 0 END,
            @counter = @counter +1 
        END
        

        PS:祝你的老化报告好运...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-14
          • 1970-01-01
          • 1970-01-01
          • 2013-08-18
          • 2017-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多