C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Databases »

Try Catch functionality in SQL Server 2005


Posted Date: 16 Jun 2009    Resource Type: Articles    Category: Databases
Author: RohitMember Level: Gold    
Rating: 1 out of 5Points: 4



I came across an interesting and useful feature of SQL server 2005, which helps in writing clear and easy SQL code.

Earlier if we have to do some transaction handling in a procedure based on the error returned in any SQL statement we used to check for error after every statement and rollback if necessary, with introduction of TRY and CATCH functionality in SQL server 2005 it has become very clear and methodical.



Earlier it was like:

Begin Transaction

SQL statement 1

If @@Error <> 0

Rollback

SQL statement 2

If @@Error <> 0

Rollback

SQL statement 3

If @@Error <> 0

Rollback

SQL statement 4

If @@Error <> 0

Rollback

Commit Transaction



Now we can do it like:

Begin TRY

Begin Transaction

SQL statement 1

SQL statement 2

SQL statement 3

SQL statement 4

Commit Transaction

End Try

Begin Catch

Rollback

End Catch



So if any error comes then the code would not execute commit and jump to catch block where we can do error handling and rollback.

Please see following link for some example.





For more details, visit http://msdn.microsoft.com/en-us/library/ms179296(SQL.90).aspx



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Try catch in Sql server 2005  .  Try  .  Catch  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: SQL Information
Previous Resource: Tips - Optimization of SQL queries
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use