0

我有一个我正在处理的存储过程,它非常简单,但给了我一些奇怪的问题。基本上,如果客户在储蓄账户上提款超过 2 次,则每次后续提款都会收取 1.00 服务费。

问题是我好像减了一个,而且只收第4次及以后交易的服务费。

这是存储过程代码:

IF @AccountType = 'Savings' AND @Type = 'W'
    BEGIN
        SELECT @WithdrawCount = WithdrawalCount
        FROM Accounts
        WHERE AccountID = @AccountID

        IF @WithdrawCount > 2
        BEGIN
            SET @ServiceCharge = 1.00

            UPDATE Accounts SET Balance = Balance - @Amount - @ServiceCharge
            WHERE Balance > 0 AND AccountID = @AccountID

            UPDATE Accounts SET WithdrawalCount = WithdrawalCount + 1
            WHERE Balance > 0 AND AccountID = @AccountID

            INSERT INTO Transactions( AccountID, EmplNo, Amount, Description, EnteredBy, ServiceCharge, Type )
            VALUES( @AccountID, @EmplNo, @Amount, @Description, @EnteredBy, @ServiceCharge, @Type )

            SET @TransID = SCOPE_IDENTITY()
            PRINT @TransID
        END
        ELSE
        BEGIN
            SET @ServiceCharge = 0.00

            UPDATE Accounts SET Balance = Balance - @Amount - @ServiceCharge
            WHERE Balance > 0 AND AccountID = @AccountID

            UPDATE Accounts SET WithdrawalCount = WithdrawalCount + 1
            WHERE Balance > 0 AND AccountID = @AccountID

            INSERT INTO Transactions( AccountID, EmplNo, Amount, Description, EnteredBy, ServiceCharge, Type )
            VALUES( @AccountID, @EmplNo, @Amount, @Description, @EnteredBy, @ServiceCharge, @Type )

            SET @TransID = SCOPE_IDENTITY()
            PRINT @TransID
        END

        SELECT @WithdrawCount = WithdrawalCount
        FROM Accounts
        WHERE AccountID = @AccountID
    END

我还完成了版本,其中每个条件(> = 3,= 3,< 3)都有一个 if 块,但也没有成功。每次我制作一个不同的版本时,一旦提款计数达到 3 就应该跳闸,它不会。任何建议都会很棒。

4

1 回答 1

2

您正在检查您的提款计数,然后再增加它。即在第 3 次,withdrawCount 的值将是 2 而不是 3,直到您更新它。只需检查它是否大于 1 或先增加它

于 2013-10-15T23:37:58.343 回答