How To Check @@ERROR to Determine If An Error Occurred In A Stored Procedure



TODO:

Have you ever wanted to check to see if an update or other error occurred in your stored procedure.  To do so check the @@ERROR variable, if it is 0, then no errors occurred.

 

SOLUTION:

IF @@ERROR = 0
BEGIN
     --return the id of the row inserted
     SELECT @@ROWCOUNT As RowsAffected
END
ELSE
BEGIN
     SELECT -1 As RowsAffected
END

 

NOTES:

In the example above, I did not care about the error code, which is why i chose to return -1 in the ELSE.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading