0

I'm trying to follow best practices and I see others doing an isnull check on @@ERROR but I was wondering if it is unnecessary. Thoughts? example:

SET @err = @@ERROR 
IF ISNULL(@err, 0) <> 0 ...

Thanks!

jarlh
  • 42,561
  • 8
  • 45
  • 63
gemArt
  • 35
  • 6

1 Answers1

0

I suggest that there is no need of ISNULL function in that Statement for that @@ERROR Variable.

Because ,it Returns 0 if the previous T-SQL statement encountered no errors.

Returns an error number if the previous statement encountered an error.

Because @@ERROR is cleared and reset on each statement executed, check it immediately following the statement being verified, or save it to a local variable that can be checked later.

Use the TRY...CATCH construct to handle errors. The TRY...CATCH construct also supports additional system functions (ERROR_LINE, ERROR_MESSAGE, ERROR_PROCEDURE, ERROR_SEVERITY, and ERROR_STATE) that return more error information than @@ERROR. TRY...CATCH also supports an ERROR_NUMBER function that is not limited to returning the error number in the statement immediately after the statement that generated an error.

Note: Also we can't set NULL to the @@ERROR Variable in T-SQL.

Thiyagu
  • 1,260
  • 1
  • 5
  • 14