SQL Server - Try Catch Statement Syntax

by Administrator 8. October 2009 21:39

I'm sure there are more complexities to this - but this seems to work well in most applications

BEGIN TRY


 BEGIN TRANSACTION
 
 -- Do whatever you need to do
 

 COMMIT TRANSACTION


END TRY


BEGIN CATCH


 -- Rollback if in a transaction


 IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
 
 -- Do whatever error logging you need to do


END CATCH

 


Buy Me a Beer ?

If you've found this site useful, you could help support its running costs by either checking out the adverts on the page if you find anything that interests you - or by making a small donation