我需要帮助才能理解为什么下面的循环不起作用。它只会插入第一个记录然后停止。
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
以下是PAYMENTS表中的当前数据:
Old_Balance | New_Balance
------------|------------
2845.8 | 2845.8
以下是表ACCOUNTS
中的当前日期Interest
--------
15.24
答案 0 :(得分:1)
@NEW_BALANCE
尚未初始化。它是NULL,不大于0(未定义)。
DECLARE
@NEW_BALANCE FLOAT
Select @NEW_BALANCE
输出:
----------------------
NULL
(1 row(s) affected)
所以,初始化它:
DECLARE
@NEW_BALANCE FLOAT = 0
Select @NEW_BALANCE