Subject: Error handling with SQL Server
Posted By: mike_remember Post Date: 1/1/2007 7:22:54 AM
Hi

I have created a stored procedure where 3 conditions are applied, like

if Condition 1 is true
select blah blah......

if Condition 2 is true
select blah blah......

if Condition 3 is true
select blah blah......

Now to handle the error, I have written with each condition that
if (@@error <> 0)
--do some work

What I want to know is, that rather than repeating the error condition in all the conditions, can't I write it universally anywhere so that if error comes in any of the conditions, some error handling is done.

Regards
Mike

Fortune favours the brave, so don't regret on missed oppurtunities.
Reply By: joefawcett Reply Date: 1/1/2007 7:32:44 AM
Unfortunately that's the only option in SQL 2000, if using SQL 2005 you can use try/catch blocks.

--

Joe (Microsoft MVP - XML)
Reply By: robprell Reply Date: 1/2/2007 6:16:56 PM
Your basically correct but here is another idea.  

Error handeling only gives you the error if you trap for it immediately after your command, similar to @@rowcount.  You can repeat a line after every command and store your error number in a variable and then do a goto an error handeling section on the bottom of your proc (I like this method).  But you have to store the error code right where it happened.  So bascially you have to put something after every command to trap the error but then you can put all your error handeling code in one area rather than immediately after the error was trapped.  I find this much more organized particularly if you want to store your errors someplace, you don't have to repeat that code that way.




Go to topic 16637

Return to index page 76
Return to index page 75
Return to index page 74
Return to index page 73
Return to index page 72
Return to index page 71
Return to index page 70
Return to index page 69
Return to index page 68
Return to index page 67