Check for Errors with @@error

The @@error global variable is commonly used to check the error status of the most recently executed batch in the current user session. @@error contains 0 if the last transaction succeeded; otherwise, @@error contains the last error number generated by the system.

A statement such as if @@error != 0 followed by return causes an exit on error.

Every Transact-SQL statement, including print statements and if tests, resets @@error, so the status check must immediately follow the batch for which success is in question.

The @@sqlstatus global variable has no effect on @@error output.

Related concepts
Check the Status From the Last Fetch