SQL Server Try catch error handling

Source: Internet
Author: User
Tags error handling try catch

A. Syntax Essentials:
BEGIN TRY
{sql_statement | statement_block}
END TRY
BEGIN CATCH
[{sql_statement | statement_block}]
END CATCH
[ ; ] 

Exception section:
within the scope of a catch block, you can use the following system functions to get the error message that caused the catch block to execute:
Error_number () returns the error number.
error_severity () returns the severity.
error_state () returns the error status number.
error_procedure () returns the name of the stored procedure or trigger where the error occurred.
Error_line () returns the line number in the routine that caused the error.
error_message () returns the full text of the error message. The text can include values provided by any replaceable parameter, such as length, object name, or time.

two. Example:
Step 1:
Create PROCEDURE Syslogerror
As
BEGIN
Insert into Systemerrorlog
(Spname,description,logtime)
Values
(Error_procedure (), Convert (nvarchar (MAX), Error_line ()) + ': ' + error_message (), GetDate ())
END

Step 2:

BEGIN TRY
BEGIN TRANSACTION;

....

COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @ @TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END

EXECUTE [dbo]. [Syslogerror];
END CATCH;


Source: http://www.cnblogs.com/Bowen80/archive/2008/07/18/1246124.html

From for notes (Wiz)

SQL Server Try catch error handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.