Click here to Skip to main content
13,196,730 members (71,596 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 7 May 2009

Good SQL Error Handling in Stored Procedure

, 7 May 2009
Rate this:
Please Sign up or sign in to vote.
Good SQL error handling in stored procedure

When writing SQL procedures, it is really important to handle errors cautiously. Having that in mind will probably save your efforts, time and money. I have been working with MS-SQL 2000 and MS-SQL 2005 (I have not got the opportunity to work with MS-SQL 2008 yet) for many years now and I want to share with you how I handle errors in T-SQL Stored Procedure. This code has been working for many years now without a hitch.

N.B.: As another "best pratice", I suggest using only ONE level of TRY ... CATCH and only ONE level of TRANSACTION encapsulation, as doing otherwise may not be 100% sure.


-- Code in transaction go here



    -- Rollback on error

    -- Raise the error with the appropriate message and error severity
    DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int;
    SELECT @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY();
    RAISERROR(@ErrMsg, @ErrSeverity, 1);


In conclusion, I will just mention that I have been using this code with .NET 2.0 and .NET 3.5 and it works like a charm. The .NET TDS parser throws back a SQLException which is ideal to work with.

Original post URL:

Category: CodeProject

Published: 5/7/2009 11:22 PM


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nicolas Brassard
President Solutions Nitriques inc.
Canada Canada
I am the general director of Nitriques Solutions inc. We build software and web solutions for enterprises using .NET 2.0 and 3.5, MS-SQL Server 2005 running on various Server 2003.

We offer a variety of products, from simple corporate web site with CMS to complete integration of intranets.

I have been working with asp et MS-SQL since 2002. I am particularly interested in web development 2.0, ajax, css, xhtml,, jQuery, Open Social and other web technologies.

In my spare time (when this concept exist in my life), i like to snowboard, skydive or free fall in wind tunnels and learning cool new stuff about anything.

Please feel free to contact me.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
KinStephen8-May-09 6:39
memberKinStephen8-May-09 6:39 
AnswerRe: My vote of 1 Pin
Nicolas Brassard9-May-09 15:45
memberNicolas Brassard9-May-09 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171019.1 | Last Updated 8 May 2009
Article Copyright 2009 by Nicolas Brassard
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid