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 use nested TRY-CATCH to catch all errors?

Execute the following SQL Server T-SQL scripts in Management Studio Query Editor to demonstrate how nesting TRY-CATCH-es catching errors at different scope.


-- SQL Server try catch nesting - nested try-catch - SQL nested TRY CATCH
USE AdventureWorks;
GO
SET NOCOUNT ON
CREATE TABLE Company(
      CompanyID INT IDENTITY(1,1) PRIMARY KEY,
      Name VARCHAR(50))
 
INSERT Company(Name) VALUES ('Ford Motor Company')
INSERT Company(Name) VALUES ('General Motors')
INSERT Company(Name) VALUES ('DaimlerChrysler')
 
CREATE TABLE ProfitCenter (
      ProfitCenterID INT IDENTITY(1,1) PRIMARY KEY,
      CompanyID INT,
      Name VARCHAR(50))
 
ALTER TABLE [dbo].[ProfitCenter] WITH CHECK
      ADD CONSTRAINT [FK_ProfitCenter_Company]
      FOREIGN KEY([CompanyID])
      REFERENCES [dbo].[Company] ([CompanyID])
GO
 
INSERT ProfitCenter (CompanyID, Name) VALUES ('1', 'Mercury')
INSERT ProfitCenter (CompanyID, Name) VALUES ('1', 'Ford')
INSERT ProfitCenter (CompanyID, Name) VALUES ('1', 'Lincoln')
INSERT ProfitCenter (CompanyID, Name) VALUES ('2', 'Chevrolet')
INSERT ProfitCenter (CompanyID, Name) VALUES ('2', 'Buick')
INSERT ProfitCenter (CompanyID, Name) VALUES ('2', 'Pontiac')
INSERT ProfitCenter (CompanyID, Name) VALUES ('2', 'Caddilac')
INSERT ProfitCenter (CompanyID, Name) VALUES ('2', 'Saturn')
INSERT ProfitCenter (CompanyID, Name) VALUES ('3', 'Mercedes')
INSERT ProfitCenter (CompanyID, Name) VALUES ('3', 'Chrysler')
 
 
CREATE TABLE PCDepartment (
      PCDepartmentID INT IDENTITY(1,1) PRIMARY KEY,
      ProfitCenterID INT,
      Name VARCHAR(50))
 
ALTER TABLE [dbo].[PCDepartment] WITH CHECK
      ADD CONSTRAINT [FK_PCDepartment_ProfitCenter]
      FOREIGN KEY([ProfitCenterID])
      REFERENCES [dbo].[ProfitCenter] ([ProfitCenterID])
 
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Sales')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Production')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Marketing')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Service')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Human Resources')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Corporate')
INSERT PCDepartment (ProfitCenterID, Name) VALUES ('1', 'Development')
 
-- Population check
 
SELECT * FROM Company
/*
CompanyID   Name
1           Ford Motor Company
2           General Motors
3           DaimlerChrysler
*/
SELECT * FROM ProfitCenter
/*
ProfitCenterID    CompanyID   Name
1                 1           Mercury
2                 1           Ford
3                 1           Lincoln
4                 2           Chevrolet
5                 2           Buick
6                 2           Pontiac
7                 2           Caddilac
8                 2           Saturn
9                 3           Mercedes
10                3           Chrysler
*/
 
SELECT * FROM PCDepartment
/*
PCDepartmentID    ProfitCenterID    Name
1                 1                 Sales
2                 1                 Production
3                 1                 Marketing
4                 1                 Service
5                 1                 Human Resources
6                 1                 Corporate
7                 1                 Development
*/
 
 
-- First execution: delete bottom level of tree
-- Second execution: delete middle level of tree
-- Third execution: delete top level of tree
 
-- SQL TRY CATCH - SQL Server TRY-CATCH error handling  
DECLARE @CompanyToDelete VARCHAR(50)
SET @CompanyToDelete = 'Ford Motor Company'
 
BEGIN TRY
      DELETE FROM Company WHERE Name = @CompanyToDelete
      PRINT 'Company '+@CompanyToDelete+' deleted successfully'
END TRY
BEGIN CATCH
      PRINT 'Error Deleting Company Record'
      BEGIN TRY
            DELETE FROM ProfitCenter WHERE CompanyID =
            (SELECT DISTINCT CompanyID FROM Company
                  WHERE Name = @CompanyToDelete)
            PRINT 'ProfitCenter Deleted Successfully'
      END TRY
      BEGIN CATCH
            PRINT 'Error Deleting ProfitCenter'
            BEGIN TRY
                  DELETE FROM PCDepartment WHERE ProfitCenterId in
                  (SELECT DISTINCT ProfitCenterID
                        FROM ProfitCenter WHERE CompanyID =
                  (SELECT DISTINCT CompanyID FROM Company
                        WHERE Name = @CompanyToDelete))
                  PRINT 'PCDepartment Deleted Successfully'
            END TRY
            BEGIN CATCH
                  PRINT 'Error Deleting PCDepartment'
                  SELECT ERROR_NUMBER() ERNumber,
                        ERROR_SEVERITY() Error_Severity,
                        ERROR_STATE() Error_State,
                        ERROR_PROCEDURE() Error_Procedure,
                        ERROR_LINE() Error_Line,
                        ERROR_MESSAGE() Error_Message
 
            END CATCH
      END CATCH
END CATCH
GO
/* Messages
 
Error Deleting Company Record
Error Deleting ProfitCenter
PCDepartment Deleted Successfully
*/
 
-- Cleanup
DROP TABLE PCDepartment
DROP TABLE ProfitCenter
DROP TABLE Company

 

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.