A small misunderstanding about @ @error in SQL Server

Source: Internet
Author: User
Tags getdate terminates

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

if (@ @error <>0)    ROLLBACK TRANSACTION telse    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:

SET NOCOUNT on; SET xact_abort on;  --Executing a Transact-SQL statement produces a run-time error, the entire transaction terminates and rolls back the begin TRANSACTION tupdate testset a= ' updated ' WHERE a= ' not updated ' RAISERROR (' Sorry, you don't have permission! ', 16, 1)

SELECT GETDATE () if (@ @error <>0) ROLLBACK TRANSACTION telse COMMIT TRANSACTION T

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:

SET NOCOUNT on; SET xact_abort on;  --Executing a Transact-SQL statement produces a run-time error, the entire transaction terminates and rolls back the begin TRY    begin TRANSACTION T update    Test    SET a= ' updated '    WHERE a= ' Not updated '    RAISERROR (' Sorry, you don't have permission! ', 16,1)    SELECT GETDATE ()        COMMIT TRANSACTION TEND trybegin CATCH    DECLARE @msg nvarchar (+) =error_message ()    --The error message that will be caught exists in the variable @msg                   RAISERROR (@msg, 16,1)    --here to throw (as if it were such a child ...)    ROLLBACK TRANSACTION T--wrong rollback end CATCH

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.