Try catch learning added in SQL server 2005

Source: Internet
Author: User
Tags exception handling try catch
The newly added try catch in SQL server 2005 can easily catch exceptions. Today I have learned about it and summarized the following points:
Basic usage: in TRY
{SQL _statement |
Statement_block}
END TRY
BEGIN CATCH
{SQL _statement |
Statement_block}
END CATCH
, Similar to exception handling in common languages, but note that SQL SERVER only captures exceptions that are not serious, such as exceptions that cannot be connected to a database, is an example that cannot be captured: BEGIN TRY
DECLARE @ X INT
-- Divide by zero to generate Error
SET @ X = 1/0
PRINT 'command after error in TRY block'
END TRY
BEGIN CATCH
PRINT 'error detected'
END CATCH
PRINT 'command after TRY/CATCH buckets'
In addition, try catch can be nested with Begin TRY
Delete from GrandParent where Name = 'John Smith'
Print 'grandparent deleted successfully'
End Try
Begin Catch
Print 'Error Deleting GrandParent Record'
Begin Try
Delete from Parent where GrandParentID =
(Select distinct ID from GrandParent where Name = 'John Smith ')
Print 'parent Deleted Successfully'
End Try
Begin Catch
Print 'Error Deleting parent'
Begin Try
Delete from child where ParentId =
(Select distinct ID from Parent where GrandParentID =
(Select distinct ID from GrandParent where Name = 'John Smith '))
Print 'child Deleted Successfully'
End Try
Begin Catch
Print 'Error Deleting Child'
End Catch
End Catch
End Catch
In addition, SQL SERVER 2005 provides methods of the error class in the exception mechanism to facilitate debugging. The following is an excerpt, which is simple and does not explain ERROR_NUMBER (): returns a number associated with the error. ERROR_SEVERITY (): Returns the severity of the error. ERROR_STATE (): Returns the error state number associated with the error. ERROR_PROCEDURE (): Returns the name of the stored procedure or trigger in which the error occurred. ERROR_LINE (): Returns the line number inside the failing routine that caused the error. ERROR_MESSAGE (): Returns the complete text of the error message. the text parameters des the values supplied for any substitutable parameters, such as lengths, object names, or times. the following example shows how to use the error class begin try.
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.