Monday, 13 March 2017

@@ERROR in sql server

Commonly used to check the error status (succeeded or failed) of the most recently executed statement. It contains 0 if the previous transaction succeeded; otherwise, it contains the last error number generated by the system.
In the following example, let's assume that an error will occur during the Update statement. @@error will contain the error code only until the next statement is executed; even the command for reading the @@error value will reset it. If it was completed successfully, SQL Server will set @@error to 0. The only way to preserve the @@error value is to immediately read it and store it in a local variable; then it can be used for error handling.
I want to update the sal value on the EMP table using below sql script
update Emp set sal='Bagesh' where EmpId=1
  
Sal type is int. defiantly it will be throw error.
See below

Here the value will change of @@ERROR variable. Now we check the value of @@ERROR


Popular Posts