2015 international technet wiki summit 2015 saeid hasani structured error handling mechanism in sql...
TRANSCRIPT
![Page 1: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/3.jpg)
3
![Page 4: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/21.jpg)
21
![Page 22: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/24.jpg)
24
![Page 25: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/26.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/27.jpg)
27
![Page 28: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/30.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/31.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/32.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/33.jpg)
33
![Page 34: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/34.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/35.jpg)
35
![Page 36: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/36.jpg)
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
![Page 37: 2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014](https://reader036.vdocument.in/reader036/viewer/2022062716/56649dc75503460f94abc315/html5/thumbnails/37.jpg)
37
See Also
Related TechNet Wiki Articles:
Error Handling within Triggers Using T-SQLT-SQL: Error Handling for CHECK Constraints
BOL link: http://technet.microsoft.com/en-us/library/ms175976.aspx