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