Use of try catch in SQL

Source: Internet
Author: User

Begin try
{SQL _statement | statement_block}
End try
Begin catch
[{SQL _statement | statement_block}]
End catch
[;]

----------------------------------------------------

Retrieve error messages

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.

      -----------------------------------------------------------

Begin try
Begin transaction
----------------------------//////////////////////////////
Commit transaction
End try
Begin catch
Rollback
Declare @ errmsg nvarchar (4000), @ errseverity int
Select @ errmsg = error_message (), @ errseverity = error_severity ()
Raiserror (@ errmsg, @ errseverity, 1)
End catch

Run the transportation Forum, run the transportation, and revitalize the economy !!! (What are you waiting !!! Hurry up and click here !!!) The webmaster's blog about transportation, transportation, and economic success !!! (What are you waiting !!! Hurry up and click here !!!)

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.