@ @ERROR: An error number is returned if the current statement encounters an error, otherwise 0 is returned. It is important to note that @error is reset immediately after each statement is executed, so you should check the value after execution of the statement you want to validate or save it in a local variable for future use.
DECLARE @num INT,@temp INTDECLARE @err INTSELECT @temp=0;SET @err=0 SET @num =2/@temp; --after execution @ @ERROR is 8134PRINT @ @ERROR --after execution @ @ERROR is 0IF @ @ERROR = 0 BEGIN PRINT @ @ERROR END
Analysis: The above SQL statement appears in addition to 0 error, throw an error message, so after the execution of the 0 statement at the @ @Error value is 8134, but after the first print @Error statement output error message, note that at this time @ @Error data immediately became 0! This is important because the @ @Error is reset immediately after each statement is executed! Similarly, the value of @ @ERROR after executing if @ @ERROR = 0 statement is still 0, because there is no error in this sentence!
@ @ERROR in SQL Server