1. Piggy Bank
Introduction:
For this project, I was responsible for creating the database and developing the T-SQL stored
procedures for banking system. The Data Access tier utilizes stored T-SQL procedures to
query and update SQL Server database tables. Within each stored procedure, I implemented
data validation and proper error handling techniques. Exceptions return a specified error
code back to the Data Access tier, where customized error handling takes place. If the stored
procedure completes without exception, values are assigned to the appropriate object
properties and passed back to the Windows Application through the business tier. The
business tier acts as a middle tier between the Windows Application tier and the Data Access
tier, handling all calls to the to the Data Access tier.
Audience:
This project is designed for bank personal to carry out typical bank functions, such as
adding accounts and making withdrawals.. The stored procedures were designed to be
easy to use.
Project Goals:
• Develop code that is easily maintainable.
• Provide adequate error handling.
• Use database-programming techniques that provide maximum programming
flexibility and control while minimizing resource utilization.
2. SQL Stored Procedure
Create Procedure
USE [PiggyBank]
GO
/****** Object: StoredProcedure [dbo].[usp_MakeWithdrawal] Script Date:
06/30/2009 13:04:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_MakeWithdrawal]
(
@AcctID int ,
@CustID int ,
@TransAmt money ,
@TransID int output
)
as
declare
@TransDt datetime ,
@CurrBal money ,
@NewBal money ,
@AcctStat tinyint ,
@OvrDftBal money ,
@OvrNewBal money ,
@GenOvrDft bit ,
@OvrDftFee money ,
@GenOvrFee money ,
@OvrDftID int ,
@wd_flg bit ,
@TransTypID tinyint
set @wd_flg = 0
set @OvrDftFee = 10
set @GenOvrFee = 30
3. Validation and Error Checking
if (@AcctID is NULL) or (@AcctID = '')
begin
raiserror('Invalid Account ID',11,1)
return
end
if (@CustID is NULL) or (@CustID = '')
begin
raiserror('Invalid Customer ID',11,1)
return
end
if (@TransAmt is null) or ( @TransAmt = '') or (@TransAmt = 0)
begin
raiserror('Incorrect Transaction Amount',11,1)
return
end
if not exists (select * from CustomerAccount where AccountID = @AcctID
and CustomerID =@CustID)
begin
raiserror('Customer does not belong to this account',11,1)
return
end
select @AcctStat = AccountStatusID from account where AccountID = @AcctID
if @AcctStat <> 1
begin
raiserror ('Account is inactive',11,1)
return
end
4. select @OvrDftID = OverDraftAccountID from Account where AccountID = @AcctID
if ((@OvrDftID is not null) or (@OvrDftID <> '')) and exists (select * from
Account where AccountID = @OvrDftID )
begin
select @OvrDftBal = CurrentBalance from Account where AccountID =
@OvrDftID
end
else
set @OvrDftBal = 0
select @CurrBal = CurrentBalance from account where AccountID = @AcctID
select @GenOvrDft = OverDraftAccountID from Account where AccountID =
@AcctID
if (@OvrDftBal is null) or (@OvrDftBal = '')
begin
set @OvrDftBal = 0
end
--set @NewBal = @CurrBal + @TransAmt
set @TransDt = GetDate()
set @TransTypID = 3
5. Insert Statements in Try/Catch Block
begin try
begin tran
if @TransAmt <= @CurrBal
begin
set @NewBal = @CurrBal - @TransAmt
insert into Transactions(AccountID, TransactionTypeID,CustomerID,
TransactionDate, TransactionAmount, NewBalance)
values(@AcctID, @TransTypID, @CustID, @TransDt, @TransAmt, @NewBal)
set @TransID = scope_identity()
update Account
set CurrentBalance = @NewBal
Where AccountID = @AcctID
set @wd_flg = 1
end
6. if (@TransAmt > @CurrBal) and (@OvrDftBal >= @TransAmt )
begin
set @OvrNewBal = @OvrDftBal - @TransAmt
insert into Transactions(AccountID, TransactionTypeID,CustomerID,
TransactionDate, TransactionAmount, NewBalance)
values(@OvrDftID, @TransTypID, @CustID, @TransDt, @TransAmt,
@OvrNewBal)
set @TransID = scope_identity()
update Account
set CurrentBalance = @OvrNewBal
Where AccountID = @OvrDftID
set @NewBal = @CurrBal - @OvrDftFee
insert into Transactions(AccountID, TransactionTypeID,CustomerID,
TransactionDate, TransactionAmount, NewBalance)
values(@AcctID, @TransTypID, @CustID, @TransDt, @OvrDftFee, @NewBal)
set @TransID = scope_identity()
update Account
set CurrentBalance = @NewBal
Where AccountID = @AcctID
update Account
set OverDraftDt = @TransDt
Where AccountID = @AcctID
set @wd_flg = 1
end
if (@TransAmt > @CurrBal) and (@TransAmt > @OvrDftBal) and (@TransAmt <
400) and ( @GenOvrDft = 1)
7. begin
set @NewBal = @CurrBal - @TransAmt
insert into Transactions(AccountID, TransactionTypeID,CustomerID,
TransactionDate, TransactionAmount, NewBalance)
values(@AcctID, @TransTypID, @CustID, @TransDt, @TransAmt, @NewBal)
set @TransID = scope_identity()
update Account
set CurrentBalance = @NewBal
Where AccountID = @AcctID
set @NewBal = @NewBal - @GenOvrFee
insert into Transactions(AccountID, TransactionTypeID,CustomerID,
TransactionDate, TransactionAmount, NewBalance)
values(@AcctID, @TransTypID, @CustID, @TransDt, @GenOvrFee, @NewBal)
set @TransID = scope_identity()
update Account
set CurrentBalance = @NewBal
Where AccountID = @AcctID
update Account
set OverDraftDt = @TransDt
Where AccountID = @AcctID
set @wd_flg = 1
end
8. if @wd_flg <> 1
begin
print 'Withdrawal Denied'
end
commit tran
end try
Error Handlin
begin catch -- raise an error to pass up the line if one was caught
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
)
end catch