2013-10-15 96 views
0

我有一個存儲過程,我正在處理這個過程非常簡單,但給我一些奇怪的問題。基本上,如果客戶在儲蓄賬戶中提款超過2次,他們在後續提款時就會收取1.00服務費。即使條件得到滿足,IF塊仍未被觸發

問題是我似乎沒有一個,它只收取第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)沒有成功,以及版本。每次我製作一個不同的版本時,一旦退出次數達到3次就應該被觸發,但它不會。任何建議都會很棒。

回答

2

您正在檢查您的提款計數,然後再增加它。即在第3次更新後,withdrawCount的值將爲2而不是3。只是檢查它是否大於1或先增加它

相關問題