tags:

views:

179

answers:

5

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!

+6  A: 

No, the CASE structure in SQL is to return a value, not for program flow. You need to break it into IF statements.

Tom H.
+6  A: 

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
Thomas
+1: For code example, though more detail would be good to explain that CASE in SQL Server is an expression, not decision logic per Remarks section in the documentation: http://msdn.microsoft.com/en-us/library/ms181765.aspx
OMG Ponies
+2  A: 

The CASE statement isn't used for branching logic in the same way as its cousin in procedural code. It will return a result in a result set so that when your assigning a value to a variable, you can determine what the value will be not which variable you are assigning to.

Unfortunately,

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

doesn't return a value.

An alternate approach using If/Else bracnching logic would be

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
Laramie
Wow. you guys are fast!
Laramie
A: 

You shouldn't use a while loop here. You are effectively only checking the value of @newBalance one time. Consider:

@monFee1 = @monthlyFee
@newBalance = @newBalance-@fee

IF @newBalance > 0
BEGIN
   @monFee2 = @monthlyFee
   @newBalance = @newBalance-@fee
END 
Tim Rooks
True nuff for two months, what happens when he goes to 12 months?
David B
Then he writes a recursive function.
Tim Rooks
A: 

Since CASE is an expression, you can use it within a SET assignment statement.

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

It can also within a SELECT assignment statement.

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: good luck with your aging report...

David B