A quick way to get error code in T-SQL (@@ERROR)
@@Error – It returns the latest error code or error number generated by last executed statement in SQL Engine. It returns integer value and this code can be found in the sys.messages catalog view.
I have a table as shown below
We got the same error code from @@Error. If the SQL statement executed without any error then we will get 0 (zero) from the @@ERROR.
It is simplest way to get the error code but we should use try…catch statement to handle errors in T-SQL. SQL Server 2012 has enhancements in error handling statements.