Home > T-SQL > A quick way to get error code in T-SQL (@@ERROR)

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.

Implementation

I have a table as shown below

pic1 I have inserted some values in to this table already

pic2Now, I am going to update the column StateID with a character value and I will get error code using @@Error.

SQL Statement

pic3We have got an error message with error code 245. Let us see what is the value in  @@Error. To do so, Go to result tab.

pic4

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.

Conclusion

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.

Advertisements
Categories: T-SQL Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: