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.
Author |
Topic |
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2012-03-14 : 11:44:05
|
I have procedure which will load and update the records.Please correct me the below for error handling for begin try,end try begin catch end catch..since it has to process 1 millions rows without impacting the perfromance..Here is the sample extract of the code.. create procedure dbo.test as DECLARE @BeginTranCount INTSET @BeginTranCount = @@TRANCOUNTIF @BeginTranCount = 0BEGIN TRANSACTIONBEGIN TRYselect * into std1 from student1END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1tableERROR' IF @@TRANCOUNT > @BeginTranCount ROLLBACK TRANSACTION RETURNEND CATCHIF @@TRANCOUNT > @BeginTranCountCOMMIT TRANSACTIONBEGIN TRYselect * into std2 from student2END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St2tableERROR' IF @@TRANCOUNT > @BeginTranCount ROLLBACK TRANSACTION RETURNEND CATCHIF @@TRANCOUNT > @BeginTranCountCOMMIT TRANSACTIONBEGIN TRYUpdate st1 set name ='test' wehre x=1END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1UpdatetableERROR' IF @@TRANCOUNT > @BeginTranCount ROLLBACK TRANSACTION RETURNEND CATCHIF @@TRANCOUNT > @BeginTranCountCOMMIT TRANSACTIONBEGIN TRYupdate st2 set zipcode='299' where zipcode is nullEND TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St2UpdatetableERROR' IF @@TRANCOUNT > @BeginTranCount ROLLBACK TRANSACTION RETURNEND CATCHIF @@TRANCOUNT > @BeginTranCountCOMMIT TRANSACTION |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-03-14 : 12:22:00
|
1. You don't need transactions because each DML statement is atomic (unless you have multiple statements in a TRY..CATACH block).2. If you want to keep the transaction statements, I'd move the BEGIN TRAN & COMMIT inside the TRY block:BEGIN TRY BEGIN TRANSACTION select * into std1 from student1 IF @@TRANCOUNT > @BeginTranCount COMMIT TRANSACTIONEND TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1tableERROR' IF @@TRANCOUNT > @BeginTranCount ROLLBACK TRANSACTION RETURNEND CATCH I'd actually write it like this:BEGIN TRY select * into std1 from student1END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1tableERROR' RETURNEND CATCH |
 |
|
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2012-03-14 : 13:36:25
|
Thanks a lot Lamprey.I have modified the code below is what i have rewritten and please let me know if anything needs to changed and is there any possiblity of deadlocks situation wiht the below query. create procedure dbo.test as BEGIN TRY select * into std1 from student1END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1tableERROR' RETURNEND CATCHBEGIN TRYselect * into std2 from student2END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St2tableERROR' RETURNEND CATCHBEGIN TRYUpdate st1 set name ='test' wehre x=1END TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St1UpdatetableERROR'END CATCHBEGIN TRYupdate st2 set zipcode='299' where zipcode is nullEND TRYBEGIN CATCH EXEC dbo.ErrorInfo_sp 'St2UpdatetableERROR' RETURNEND CATCH |
 |
|
|
|
|