Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a CASE statement within a WHILE loop?

This is what I'm doing:

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

I get this error:

Incorrect syntax near the keyword 'CASE'.

No idea why. Please help!

like image 877
John Avatar asked May 26 '10 19:05

John


1 Answers

For what you are proposing, you should use IF statements

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
like image 119
Thomas Avatar answered Sep 30 '22 02:09

Thomas