A small misunderstanding about @ @error in SQL Server

Source: Internet
Author: User
Tags getdate terminates

In the original: a small misunderstanding about @ @error in SQL Server

In SQL Server, I often see some predecessors write this:

if (@ @error<>0)     ROLLBACK TRANSACTION T Else    COMMIT TRANSACTION T

At first, I saw someone else write it, and I took it for granted that it was just a counter, and whenever an error was detected, the @ @error value of +1, but for that matter, the cup has ...

In fact, it is not a counter, it is a dynamic value, dynamically identifies the result of the last SQL command execution, or 0 if successful, and identifies the error code if it is unsuccessful. So, like the above writing is inappropriate, for example, as follows:

SETNOCOUNT on;SETXact_abort on;--executing a Transact-SQL statement produces a run-time error, the entire transaction terminates and rolls backBEGIN TRANSACTIONTUPDATETestSETA='has been updated'WHEREA='not updated'RAISERROR('Sorry, you don't have permission! ', -,1)

SELECT GETDATE()if(@ @error<>0) ROLLBACK TRANSACTIONTElse COMMIT TRANSACTIONT

Analysis:

According to my previous understanding, "RAISERROR (' Sorry, you don't have permission!") ', 16, 1) ' This throws an error, and the whole transaction should be rolled back, but it's not rolling back!! So what's the reason? Originally, the problem is "select GETDATE ()" This sentence above! Because the value of @ @error is not 0 (as if it were 5000) when executing the RAISERROR statement, the @ @error value becomes 0 when executed to the next sentence "Select GETDATE ()"! Therefore, the following if statement naturally does not catch any errors ...

Countermeasures:

Now that we have found the cause, the solution is natural. With a Try ... The catch syntax is right, with the following statement:

SETNOCOUNT on;SETXact_abort on;--executing a Transact-SQL statement produces a run-time error, the entire transaction terminates and rolls backBEGINTRYBEGIN TRANSACTIONTUPDATETestSETA='has been updated'    WHEREA='not updated'    RAISERROR('Sorry, you don't have permission! ', -,1)    SELECT GETDATE()        COMMIT TRANSACTIONTENDTRYBEGINCATCHDECLARE @msg nvarchar( -)=Error_message ()--The error message to be caught exists in the variable @msg    RAISERROR(@msg, -,1)--you can throw it here (like this ...)    ROLLBACK TRANSACTIONT--Wrong back to the rolling businessENDCatch

A small misunderstanding about @ @error in SQL Server

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.