我有一个我正在处理的存储过程非常简单,但却给了我一些奇怪的问题。基本上,如果客户在储蓄账户上提款超过2次,则每次提款时他们会收取1.00服务费。
问题在于我似乎只有一个人,它只收取第四次及以后交易的服务费。
以下是存储过程代码:
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就应该被绊倒,但事实并非如此。任何建议都很棒。
答案 0 :(得分:2)
在递增之前,您正在检查提款计数。即第3次,在更新之前,withdrawCount的值将为2而不是3。只需检查它是否大于1或先增加