sql server - While Loop in SQL not working -


i need in understanding why loop below not working. inserts first record 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     accounts account = 'amexdelta'  set @monthly_rate               = (@rate / 12) / 100  select top 1 @cur_new_balance       = new_balance              , @cur_old_balance     = old_balance payments order id desc  select @cur_new_balance new_balance        , @cur_old_balance 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 payments(payment, monthly_interest, old_balance, new_balance)         select @payment, @monthly_interest, @old_balance, @new_balance   end 

below current data in table payments:

old_balance | new_balance ------------|------------ 2845.8      | 2845.8 

below current date in table accounts

interest -------- 15.24 

@new_balance hasn't been initialized. it's null, not greater 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 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -