SQL Server - What is the difference between @@ ERROR and ERROR_NUMBER ()?

Views: 791
Reading Time: 3 minutes

Hey guys!
2019's first post!

In this article, I will explain the difference between @@ ERROR and the ERROR_NUMBER () function, which in theory bring the same result, that is, the error number caused by some operation in the current session. The idea of ​​writing about it came from a doubt of a Nigerian in the group SQL Server - DBAfrom Telegram and thought it would be nice to have this explanation posted on my blog to help others with the same question.

First, let's consult the official Microsoft documentation to see if we have any hints of the difference of these 2 methods:

@@ ERROR

Returns the error number of the last Transact-SQL statement executed. Returns 0 if the previous Transact-SQL statement did not find any errors.

Once @@ ERROR is erased and reset on each statement executed, refer to it immediately after the statement is checked or save it to a local variable that can be checked later.

ERROR_NUMBER

This function returns the error number that caused the CATCH block of a TRY… CATCH construct to execute. When called on a CATCH block, ERROR_NUMBER returns the number of the error that caused the CATCH block to execute. ERROR_NUMBER returns NULL when called out of scope of a CATCH block.

ERROR_NUMBER returns a relevant error number, regardless of how many times or where it is executed within the scope of the CATCH block. It is different from a function like @@ ERROR, which returns only one error number in the statement immediately after the one that causes an error.

In a nested CATCH block, ERROR_NUMBER returns the error number specific to the scope of the CATCH block that referenced that CATCH block. For example, the CATCH block of an external TRY ... CATCH construct could have an internal TRY ... CATCH construct. Within this internal CATCH block, ERROR_NUMBER returns the error number that invoked the internal CATCH block. If ERROR_NUMBER is executed on the external CATCH block, it returns the error number that invoked that external CATCH block.

Looking at the documentation, it is clear that these 2 error code identification methods differ:

  • @@ ERROR: Returns the error code of the last statement executed (with each new statement, the variable is reset). Returns 0 if it has no error or if the variable is zeroed. It is recommended to assign the @@ ERROR result to a local variable at the very beginning of CATCH, because ANY COMMAND causes the @@ ERROR to be reset (even a SELECT @@ ERROR).
  • ERROR_NUMBER: Returns the error code within the scope of a CATCH block. Returns NULL if you try to use the command outside a CATCH block.

And now, let's see this working in practice!

Result:

But what if I am not using TRY..CATCH and want to capture the error code? Can I use either method?

Answer: NO! If not within a TRY .. CATCH block, the ERROR_NUMBER () function will return NULL, while the @@ ERROR variable will return the error code (remember @@ ERROR's conditions of use to prevent it from being reset and you miss this information).

And that's it folks!
Post very short and objective and I hope it helped clarify the difference between @@ ERROR and ERROR_NUMBER ().
And remember: USE TRY ... CATCH!

A good 2019 for you and until the next article!
Big hug!