The simplest method for handling errors in T-SQL is very similar to the "on error resume next" struct we're stuck with in ASP:
BEGIN TRANSACTION -- statement that might cause error IF @@ERROR != 0 BEGIN ROLLBACK TRANSACTION RAISERROR('There was an error here.', 11, 1) RETURN END ELSE COMMIT TRANSACTION |
This is not pretty, and will not catch all errors (some errors, depending on their severity, will terminate the batch before you can even catch the value from @@ERROR).
For a much more thorough discussion of error handling in SQL Server, see Erland's articles: