0

I need help in understanding why is the loop below not working. It only inserts the first record it then stops.

DECLARE 
@NEW_BALANCE            FLOAT
, @CUR_NEW_BALANCE      FLOAT
, @OLD_BALANCE          FLOAT
, @CUR_OLD_BALANCE      FLOAT
, @INITIAL_BALANCE      FLOAT
, @RATE                 FLOAT
, @MONTHLY_RATE         FLOAT
, @MONTHLY_INTEREST     FLOAT
, @PAYMENT              FLOAT



SELECT @RATE    = INTEREST
    FROM ACCOUNTS WHERE ACCOUNT = 'AMEXDELTA'

SET @MONTHLY_RATE               = (@RATE / 12) / 100

SELECT TOP 1 @CUR_NEW_BALANCE       = NEW_BALANCE
             , @CUR_OLD_BALANCE     = OLD_BALANCE
FROM PAYMENTS ORDER BY ID DESC

SELECT @CUR_NEW_BALANCE AS NEW_BALANCE
       , @CUR_OLD_BALANCE AS OLD_BALANCE

WHILE @NEW_BALANCE > 0
BEGIN

    SET @MONTHLY_INTEREST               = (@CUR_OLD_BALANCE * @MONTHLY_RATE)

    SET @OLD_BALANCE                    = (@CUR_NEW_BALANCE - @PAYMENT)

    SET @NEW_BALANCE                    = (@MONTHLY_INTEREST + @OLD_BALANCE)


    INSERT INTO PAYMENTS(PAYMENT, MONTHLY_INTEREST, OLD_BALANCE, NEW_BALANCE)
        SELECT @PAYMENT, @MONTHLY_INTEREST, @OLD_BALANCE, @NEW_BALANCE


END

Below is the current data in the table PAYMENTS:

Old_Balance | New_Balance
------------|------------
2845.8      | 2845.8

Below is the current date in table ACCOUNTS

Interest
--------
15.24
6
  • @MartinSmith I put it back. I didn't want to post without thoroughly checking. After re-reading, I didn't understand why it even worked the first time... so I pulled it back. Commented Feb 1, 2014 at 0:58
  • @CJBS Yes the question must be wrong. The while is never true. So it is executed 0 times not 1. Commented Feb 1, 2014 at 0:59
  • I remove the loop and I'm still getting null values for old_balance and new balance Commented Feb 1, 2014 at 1:03
  • You never initialise @payment either. Commented Feb 1, 2014 at 1:04
  • 1
    @jorame - I suggest doing a bit more debugging, either in the IDE, or by using some PRINT statements to output the value of your variables at different stages of execution. Most likely the other problems are also related to variable-initialization. Float variables in T-SQL aren't auto-initialized to 0 as they might be in C#. Commented Feb 1, 2014 at 1:05

1 Answer 1

1

@NEW_BALANCE hasn't been initialized. It's NULL, which is not greater than 0 (it's undefined).

DECLARE 
@NEW_BALANCE            FLOAT

Select @NEW_BALANCE

Output:

----------------------
NULL

(1 row(s) affected)

So, initialize it:

DECLARE 
@NEW_BALANCE            FLOAT = 0

Select @NEW_BALANCE
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that it was inserting one record before I initialized it. I did as you have suggested and still the same issue. I get one record with NULL values in Old_Balance and New_Balance fields in the Payments table.
Might be a problem with the overall code logic. Have a look at: msdn.microsoft.com/en-us/library/ms178642.aspx for some examples...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.