Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Try-Catch in Sql 2005 is your friend.

I know that SQL 2005 has been out for a while now, but I did not do ANY sql for over a year.  Anyway, anyone that has had to write anything more than trival procs knows that error handling can be a pain.

In the past you have had to do something along the line of the following

IF ( @@ERROR > 0 )
-- Do Something

A better way is to wrap your code in Try-Catch and with SQL 2005 you can do this pretty easily.

BEGIN TRY

-- .... logic goes here

END TRY
BEGIN CATCH
-- handle here, or simply return
-- here I wanted to output the error, also showing what can be done
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH

One really nice advantage to using the Try-Catch is that errors thrown in subsequent proc calls can be caught and handled.  No more needing to pass back return codes from procs.

Check out the documentation for more information - found here

Till next time,


Posted 01-18-2008 9:00 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

DotNetKicks.com wrote SQL 2005 Try-Catch is your friend
on 01-18-2008 10:02 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

» Daily Bits - January 19, 2008 (Early Edition) Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote » Daily Bits - January 19, 2008 (Early Edition) Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on 01-18-2008 9:15 PM

Pingback from  » Daily Bits - January 19, 2008 (Early Edition) Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

Javier-Romero wrote links for 2008-01-22
on 01-22-2008 4:27 AM

links for 2008-01-22

No prescription phentermine. wrote Phentermine without a prescription.
on 08-05-2008 2:46 AM

No prescription phentermine. Phentermine 37.5 mg online prescription. Lowest price phentermine no prescription.

Pharme983 wrote re: Try-Catch in Sql 2005 is your friend.
on 08-01-2009 9:36 AM

Very nice site!

Pharme876 wrote re: Try-Catch in Sql 2005 is your friend.
on 08-10-2009 9:06 PM

Very nice site!

Pharmd517 wrote re: Try-Catch in Sql 2005 is your friend.
on 08-10-2009 9:08 PM

Very nice site!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)