SQL Server try catch error handling

Source: Internet
Author: User
Tags try catch
I. Syntax points:
Begin try
{SQL _statement extends statement_block}
End try
Begin catch
[{SQL _statement extends statement_block}]
End catch
[;]

Exception section:
Within the scope of the catch block, you can use the following system functions to obtain the error message that causes the Catch Block to be executed:
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 with an error.
Error_line () returns the row number in the routine that causes the error.
Error_message () returns the complete text of the error message. This text can include any value provided by replaceable parameters, such as length, object name, or time.

II. Instance:
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;

# Database Technology

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.