Raising an error message in SQL Server stored procedure

It may be good idea to put a try and catch around the stored procedure block to avoid any unexpected crash of any of the applications. There are different ways to handle errors in database.
Here is an example on how i would handle the errors in stored procedures.



Code:
BEGIN TRY 

--- your sql code here

END TRY
     BEGIN CATCH
         
      -- catch an exception            
      DECLARE @ErrMsg nvarchar(4000)
                 , @ErrSeverity int;
           SELECT @ErrMsg = ERROR_MESSAGE()
                 , @ErrSeverity = ERROR_SEVERITY()

           RAISERROR(@ErrMsg, @ErrSeverity, 1)
    END CATCH   



Happy coding!

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net