Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shmecher
Starting Member
10 Posts |
Posted - 2005-01-26 : 11:06:55
|
| Hi. I'm trying to come up with a plan on exception handling in my stored procedures, and I have some questions.First of all, I went through some documents explaining exception handling in SQL Server (http://www.sommarskog.se/error-handling-I.html, http://www.sommarskog.se/error-handling-II.html) and I came up with the following plan:1. check the @@error variable after each statement (INSERT, UPDATE, DELETE, CREATE temp_table, SELECT INTO temp_table) in all my stored procedures and return the error (upon the first error)eg UPDATE some_table...SELECT @err = @error IF @err<>0 RETURN @err2. check @@error after calling a stored procedure and return the errorEXEC @err = some_spSELECT @err = isnull(@err, @@error) IF @err<>0 RETURN @errI've ommited transaction rollback into this check, because every time I start my procedures (whose task is to load some tables with given data) I have an unload procedure that simulates rollbacks.I do handle dynamic SQL and function differently (like in the articles). Data errors (eg delete from a table and rowcount is 0) are also handled differently.Am I missing something? Is there an essential validation in building an exception architecture that I'm missing? Is there anything else that I should consider?Thanks,TIBM |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-01-26 : 13:15:11
|
| Well...I would reccomend against using the Return value as a check. SQL Server could override that value, and if you could for a specific value, you could get erroneous results.Better to use an output variableAnd there are 3 types of errors (I Think).1. User errors, like the one you mentioned...I want to do an update but @@ROWCOUNT says nothing got updated2. System Errors that return their values to @@ERROR, and3. Fatal errors that just raise out and you have no control over..that's where checking the return value is helpfulI use this template almost exclusivelyhttp://weblogs.sqlteam.com/brettk/archive/2004/05/25/1378.aspxAnd why would you want to mimic a rollback...just let sql server handle that for you..because you are just doubling the tlogs that wayBrett8-) |
 |
|
|
|
|
|
|
|