Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Libraries of Stored Procedures and Error Handling

 
   Database Help (Home) -> Programming RSS
Next:  System file watcher SSIS item?  
Author Message
agendum97

External


Since: Aug 16, 2008
Posts: 5



(Msg. 1) Posted: Sat Aug 16, 2008 12:38 pm
Post subject: Libraries of Stored Procedures and Error Handling
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I am trying to design a high quality set of library stored procedures
in MSSQL 2005. My issue at hand is I don't know the best way to
design error handling. For example, suppose I have the following
stack of executed stored procedures in TSQ:

Client (C# .NET)
> PrimaryStoredProcedure (BEGIN/END TRANSACTION)
> LibraryStoredProcedureA
> LibraryStoredProcedureB (RAISERROR ...)

LibraryStoredProcedureB does a RAISERROR and then RETURN. But this
design requires LibraryStoredProcedureA to check for the @@ERROR and
then RETURN, and then PrimaryStoredProcedure to somehow check the
@ERROR (even thought it was cleared) and ROLLBACK.

It just seems like a lot of work. The only good alternative I've come
up with is to wrap each stored procedure in a TRY/CATCH, and then
"rethrow" it back to the caller (even though there doesn't seem to be
any way to rethrow in TSQL). I know you can rethrow by calling
RAISERROR again, as shown at
http://sqlblog.com/blogs/roman_rehak/archive/2007/12/01/how-to-rethrow...rors-in
but for system exceptions I now lose the severity.

If a RAISERROR occurs, ultimately I want the Client to get the
exception.

Is there a better way to implement this than what I've just outlined?

 >> Stay informed about: Libraries of Stored Procedures and Error Handling 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Sat Aug 16, 2008 3:50 pm
Post subject: Re: Libraries of Stored Procedures and Error Handling [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

( ) writes:
> It just seems like a lot of work. The only good alternative I've come
> up with is to wrap each stored procedure in a TRY/CATCH, and then
> "rethrow" it back to the caller (even though there doesn't seem to be
> any way to rethrow in TSQL). I know you can rethrow by calling
> RAISERROR again, as shown at
> http://sqlblog.com/blogs/roman_rehak/archive/2007/12/01/how-to-rethrow-
> errors-in-t-sql.aspx
> but for system exceptions I now lose the severity.
>
> If a RAISERROR occurs, ultimately I want the Client to get the
> exception.
>
> Is there a better way to implement this than what I've just outlined?

Go for TRY-CATCH. You can pick up the severity with error_severity
and format that into your error message, which the client has to parse.

Trying to use @@error means a lot of work, and sometimes it does not
work at all.

I don't know if you have read my articles on error handling. They are in
dire need of an update for SQL 2005, but some of the basics may be of
interest to you: http://www.sommarskog.se/error-handling-I.html.


--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: Libraries of Stored Procedures and Error Handling 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
OPENQUERY with stored procedures error - I am currently using SQL Server 2005 Express Edition and SSMSE. I have developed a unit testing suite for my stored procedures. The way it works is, all of my tests (stored procedures on the database that call the actual stored procedures to..

CLR stored proc ececution, missing Libraries - Hi, I've developed a small .net application library. Now I am trying to call the library from within a CLR stored proc. I've managed to make the stored proc working OK. However,I can't call the my ..NET component. To do this I need access to library...

Exception handling in Stored procedure - Hi All, Conside this senario, (I am using SQL Server 2000) I have to process multiple orders and I have a store procedures to do this. This is schema/algorithm of my store proc Create Proc ProcessOrders Return ErrorStatus varchar(2000) Begin --....

Handling stored procedure for multiple databases - Hi, I am trying to work on a stored procedure that will work with multiple database. I have a prototype of multiple databases. Those are named as the following: ts2_aldkm_app, ts2_aldkp_app, ts2_aldkt_app. The middle part of the database name..

stored procedures - Is it possible, if so, how do you exec a SP and have the results from it be usable so it could be joined into another table? Even if I have to store the results into a temp table first is fine.....
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]