Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 1st, 2007, 08:22 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default Error handling with SQL Server


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.


Fortune favours the brave, so don't regret on missed oppurtunities.
  #2 (permalink)  
Old January 1st, 2007, 08:32 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

Unfortunately that's the only option in SQL 2000, if using SQL 2005 you can use try/catch blocks.


Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old January 2nd, 2007, 07:16 PM
Friend of Wrox
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts

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.

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server Reg. SQL Server does not exist error Arsi SQL Server 2000 1 June 11th, 2008 11:20 AM
C# handling SQL Server exceptions booksnore2 General .NET 3 September 16th, 2006 09:40 AM
error handling in ms sql starnet SQL Server 2000 1 July 19th, 2006 12:01 PM
SQL Server Error azwildcat4ever SQL Server 2000 3 December 22nd, 2004 11:36 AM
Server.Execute() Error handling nickelsberry Classic ASP Professional 2 April 14th, 2004 04:45 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.