2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL...

37
International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014

Transcript of 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL...

Page 1: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

International TechNet Wiki Summit2015

Saeid Hasani

Structured Error Handling Mechanism in SQL Server 2012 & 2014

Page 2: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

2

Senior Database DeveloperSQL Server ConsultantFreelance SQL AuthorT-SQL TrainerTechnet and MSDN SQL Server Forums Moderator

Saeid Hasani

Page 3: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

3

Page 4: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

4

TRY• Try executing statements  CATCH• Handle the errors if they occur

Is there any structured Error Handling mechanism in SQL Server?

Page 5: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

5

Is there any structured Error Handling mechanism in SQL Server?

BEGIN TRY -- Start to try executing statements SELECT 1 / 0; /* Executing statements */END TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs PRINT 'Error occurs!' /* Handle the error */END CATCH -- End of Handling the error if occurred

Page 6: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

6

Is there any structured Error Handling mechanism in SQL Server?

Page 7: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

7

Will all statements in TRY block try to execute?

BEGIN TRY -- Start to try executing statements PRINT 'Before Error!' -- Statement no1 SELECT 1 / 0; -- Statement no2 PRINT 'After Error!' -- Statement no3END TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs PRINT 'Error occurs!' /* Handle the error */END CATCH -- End of Handling the error if occurred

Page 8: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

8

Will all statements in TRY block try to execute?

Page 9: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

9

Does the CATCH part automatically handle the

errors?

BEGIN TRY -- Start to try executing statements SELECT 1 / 0; -- StatementEND TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs END CATCH -- End of Handling the error if occurred

Page 10: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

Does the CATCH part automatically handle the

errors?

10

Page 11: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

11

Does the CATCH part automatically handle the errors?

TRY• Try executing statements CATCH• Handle the error if occurs• RAISERROR

Page 12: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

12

Does the CATCH part automatically handle the errors?

BEGIN TRY -- Start to try executing statements SELECT 1 / 0; -- StatementEND TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs RAISERROR('Error!!!', 16, 1);END CATCH -- End of Handling the error if occurred

Page 13: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

13

Does the CATCH part automatically handle the errors?

Page 14: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

14

Does the CATCH part automatically handle the errors?TRY• Try executing statements CATCH• Handle the error if occurs• RAISERROR• ERROR_NUMBER()• ERROR_MESSAGE()• ERROR_SEVERITY()• ERROR_STATE()• ERROR_PROCEDURE()• ERROR_LINE()

Page 15: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

15

Does the CATCH part automatically handle the errors?BEGIN TRY -- Start to try executing statements SELECT 1 / 0; -- StatementEND TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs 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 -- End of Handling the error if occurred

Page 16: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

16

Does the CATCH part automatically handle the errors?

Page 17: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

17

Is it a good idea to use a general procedure as a

modular Error Handler routine?CREATE PROCEDURE spErrorHandlerASSET NOCOUNT ON; 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. );

Page 18: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

Is it a good idea to use a general procedure as a

modular Error Handler routine?CREATE PROCEDURE spTestASSET NOCOUNT ON;BEGIN TRY -- Start to try executing statements SELECT 1 / 0; -- StatementEND TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs EXEC spErrorHandler;END CATCH -- End of Handling the error if occurredgo------------EXEC spTest

18

Page 19: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

19

Is it a good idea to use a general procedure as a modular Error Handler routine?

Page 20: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

20

Is it a good idea to use a general procedure as a modular Error Handler routine?CREATE PROCEDURE spTest AS SET NOCOUNT ON;BEGIN TRY -- Start to try executing statements SELECT 1 / 0; -- StatementEND TRY -- End of trying to execute statementsBEGIN CATCH -- Start to Handle the error if occurs 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 -- End of Handling the error if occurred

Page 21: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

21

Page 22: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

22

What are the benefits of THROW

when we have RAISERROR?

Page 23: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

23

1. Correct line number of the error!

CREATE PROCEDURE spTest AS SET NOCOUNT ON;

BEGIN TRY SELECT 1/0END TRYBEGIN CATCH declare @msg nvarchar(2000) = error_message();

raiserror( @msg , 16, 1); THROW END CATCH

Page 24: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

24

Page 25: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

25

2. Easy to use

Another benefit of using the THROW statement is that there is no need for extra code in RAISERROR.

Page 26: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

26

3. Real Terminator!

BEGIN CATCH DECLARE @msg NVARCHAR(2000) = ERROR_MESSAGE(); RAISERROR( @msg , 16, 1); ----------------------------- CREATE TABLE #Saeid (id INT) INSERT #Saeid VALUES ( 101 ); SELECT * FROM #Saeid; DROP TABLE #Saeid; ----------------------------- THROW PRINT 'This will never print!!!';END CATCH

Page 27: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

27

Page 28: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

28

4. Independence of sys.messages

CREATE PROC sptest AS SET NOCOUNT ON;BEGIN TRY SELECT 1/0END TRYBEGIN CATCH THROW 60000, 'This a custom message!', 1; END CATCH

Page 29: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

29

4. Independence of sys.messages

Page 30: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

30

I want to check a condition in the TRY block. How

can I control the flow of execution and raise the

error?CREATE PROC sptest AS SET NOCOUNT ON;BEGIN TRY THROW 60000, 'This a custom message!', 1; END TRYBEGIN CATCH THROW END CATCH

Page 31: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

31

I want to check a condition in the TRY block. How can I control the flow of execution and raise the error?

Page 32: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

32

Does the CATCH part automatically rollback the

statements within the TRY part?

CREATE PROC sptest AS SET NOCOUNT ON;BEGIN TRY CREATE TABLE dbo.Saeid --No1 ( id int ); SELECT 1/0 --No2END TRYBEGIN CATCH THROW END CATCHgo------------------------------------->

EXEC sptest;SELECT *FROM dbo.Saeid;

Page 33: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

33

Page 34: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

34

Can someone use TRANSACTION in the TRY/CATCH block?

CREATE PROC sptest AS SET NOCOUNT ON;BEGIN TRY SET XACT_ABORT ON; --set xact_abort option BEGIN TRAN --begin transaction CREATE TABLE dbo.Hasani ( id int ); SELECT 1/0 COMMIT TRAN --commit transactionEND TRYBEGIN CATCH IF @@TRANCOUNT > 0 --check if there are open transaction? ROLLBACK TRAN; --rollback transaction THROW END CATCH------------------------------------->EXEC sptest;

SELECT *FROM dbo.Saeid;

Page 35: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

35

Page 36: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.

36

Can someone use TRANSACTION in the TRY/CATCH block?

TRY block• XACT_ABORT• Begin transaction• Statements to try

• Commit transactionCATCH block• Check @@TRANCOUNT and rollback all transactions• THROW