SQL transaction, in sql2000 to determine whether execution is successful with @ @ERROR judgment

Source: Internet
Author: User

The original: SQL transaction, in sql2000 to determine whether the execution is successful with @ @ERROR judgment

Put a SQL transaction in sql2000 to determine if execution succeeds with @ @ERROR
This thing is still a bit of a problem, sql2005 can use try. Catch, regardless of the error can be captured, and then in the rollback, very convenient, posted out drying

BEGIN TRANSACTION;

BEGIN TRY--try Execute SQL

SELECT 1/0;

END TRY
BEGIN catch--cache catch wrong
SELECT
Error_number () as ErrorNumber,--return error number
Error_severity () as errorseverity,--return severity
Error_state () as ErrorState,--Return error status number
Error_procedure () as Errorprocedure,--returns the name of the stored procedure or trigger in which the error occurred
Error_line () as ErrorLine,--Returns the line number in the routine that caused the error
Error_message () as errormessage;--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

IF @ @TRANCOUNT > 0---Returns the number of active transactions for the current connection.
ROLLBACK TRANSACTION; --The transaction in error is to be rolled back
END CATCH;

IF @ @TRANCOUNT > 0---Returns the number of active transactions for the current connection.
Commit transaction;--the correct SQL, commit the transaction
GO

SQL transaction, in sql2000 to determine whether execution is successful with @ @ERROR judgment

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.