Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Try..Catch Best Practices

Author  Topic 

Zack
Starting Member

26 Posts

Posted - 2008-08-27 : 14:31:04
Greetings,

I'm in the process of migrating several databases from SQL Server 2000 to 2005 and part of that migration process is to use new features in SQL Server 2005. What I'd like to know is when do you folks use try..catch blocks in your stored procedures? Given a CRUD scenario for a table do you use try..catch in all cases? My initial plan is as follows:

Create - use try..catch because there could be an error adding record to table
Retrieve - try..catch not needed
Update - use try..catch for same reasons as Create
Delete - try..catch not needed

Thanks in advance for your feedback.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-27 : 14:37:54
Only depending on requirements. Suppose if we want updations to be done on three tables and reqmnt is like either three of them should happen or if error occurs in middle then none should be effected. in such case, we wrap the updates in a transaction inside try block and inside catch block we will rollback transaction to remove all changes occured before an error happened.
Go to Top of Page

Zack
Starting Member

26 Posts

Posted - 2008-08-28 : 07:54:48
At this point I'm trying to define my requirements :). One thing I plan to do is review the existing stored procedures to see if there's any error handling now. If there is I'll convert it to try..catch and if there isn't I may add it depending on what the SP is doing.


"You never fail, you simply produce results. Learn from these" - Anonymous
Go to Top of Page
   

- Advertisement -