datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to TRY-CATCH in money transfer?

Execute the following SQL Server T-SQL example scripts in Management Studio Query Editor to create and execute a stored procedure for banking try-catch error control demonstration.

-- SQL Server TRY CATCH - T-SQL try catch error handling - SQL stored procedure

USE tempdb

GO

SET NOCOUNT ON

-- Balance column has check constraint - SQL check constraint

CREATE TABLE CheckingAccount (

  CustomerID INT    IDENTITY ( 1 , 1 ),

  Balance    MONEY,

     CHECK ( Balance >= 0 ),

  TranDate datetime default(getdate()))

 

CREATE TABLE SavingAccount (

  CustomerID INT    IDENTITY ( 1 , 1 ),

  Balance    MONEY,

     CHECK ( Balance >= 0 ),

  TranDate datetime default(getdate()))

GO

 

INSERT CheckingAccount(Balance) SELECT 2000

INSERT CheckingAccount(Balance) SELECT 1000

INSERT CheckingAccount(Balance) SELECT 3000

INSERT CheckingAccount(Balance) SELECT 1000

INSERT CheckingAccount(Balance) SELECT 3500

INSERT CheckingAccount(Balance) SELECT 1500

INSERT SavingAccount(Balance) SELECT 3000

INSERT SavingAccount(Balance) SELECT 6000

INSERT SavingAccount(Balance) SELECT 2000

INSERT SavingAccount(Balance) SELECT 5000

INSERT SavingAccount(Balance) SELECT 2300

INSERT SavingAccount(Balance) SELECT 5300

GO

SELECT * FROM SavingAccount ORDER BY CustomerID

/*

CustomerID  Balance     TranDate

1     3000.00     2019-02-25 10:05:35.170

2     6000.00     2019-02-25 10:05:35.187

3     2000.00     2019-02-25 10:05:35.187

4     5000.00     2019-02-25 10:05:35.187

5     2300.00     2019-02-25 10:05:35.187

6     5300.00     2019-02-25 10:05:35.187

*/

 

DROP PROCEDURE uspTransferFromSavingsToCheckingAccount

GO

 

CREATE PROCEDURE uspTransferFromSavingsToCheckingAccount

                @CustomerID INT,

                @Amount     MONEY

AS

  BEGIN

    SET XACT_ABORT  ON

    SET NOCOUNT ON

    

    BEGIN TRY

      BEGIN TRAN

      

      UPDATE CheckingAccount

      SET    Balance = Balance + @Amount

      WHERE  CustomerID = @CustomerID

      

      UPDATE SavingAccount

      SET    Balance = Balance - @Amount

      WHERE  CustomerID = @CustomerID

      

      PRINT 'TRANSFER SUCCESSFUL.'

      

      COMMIT

    END TRY

    

    BEGIN CATCH

      ROLLBACK

      

      PRINT 'INSUFFICIENT AMOUNT IN SAVINGS.'

    END CATCH

  END

 

GO

 

-- Execute stored procedure - Insufficient balance to do transfer

-- Check constraint will be violated - CATCH will kick in

EXEC uspTransferFromSavingsToCheckingAccount   3 ,   3000

GO

/* Messages

 

INSUFFICIENT AMOUNT IN SAVINGS.

 

*/

-- Sufficient balance to do transfer

SELECT Balance FROM SavingAccount WHERE CustomerID=3

-- 2000.00

SELECT Balance FROM CheckingAccount WHERE CustomerID=3

-- 3000.00

EXEC uspTransferFromSavingsToCheckingAccount   3 ,   2000

SELECT Balance FROM SavingAccount WHERE CustomerID=3

-- 0.00

SELECT Balance FROM CheckingAccount WHERE CustomerID=3

-- 5000.00

GO

/* Messages

 

TRANSFER SUCCESSFUL.

*/

 

-- Cleanup

DROP TABLE CheckingAccount

DROP TABLE SavingAccount

GO

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.