- Table -
ID int
AccountNumber int
ReceivedDate smalldatetime
Credit decimal(9,2)
Debit decimal(9,2)
Balance decimal(9,2)
- Stored Procedure -
CREATE PROCEDURE DoTransaction @AccountNumber INT, @Amount DECIMAL(9, 2)
AS
DECLARE @Credit DECIMAL(9, 2)
DECLARE @Debit DECIMAL(9, 2)
DECLARE @Balance DECIMAL(9, 2)
SET @Balance = 0
IF @Amount > 0
BEGIN
SET @Debit = 0
SET @Credit = @Amount
END
ELSE
BEGIN
SET @Debit = ABS(@Amount)
SET @Credit = 0
END
SELECT TOP 1 @Balance=Balance FROM BrokerTransactions WHERE AccountNumber=@AccountNumber ORDER BY ID DESC
INSERT INTO BrokerTransactions VALUES(@AccountNumber, GETDATE(), @Debit, @Credit, @Balance + @Amount)