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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-02-22 : 09:54:45
|
| Urgent writes "If you have Stored Procedure, which has a number of INSERT, UPDATE and etc. batches how I should perform TRANSACTION Error Handling in the right way?1. EXAMPLE BBEGIN TRANSACTION A...INSERT...UPDATE...DELETE....IF @@ERROR<>0ROLLBACK TRANSACTION AELSECOMMIT TRANSACTION A2. EXAMPLE BBEGIN TRANSACTION A...INSERT...IF @@ERROR<>0 GOTO ERR...UPDATE...IF @@ERROR<>0 GOTO ERR...DELETE....IF @@ERROR<>0 GOTO ERR...IF @@ERROR=0 GOTO SUCERR:IF @@ERROR<>0ROLLBACK TRANSACTION ASUC:COMMIT TRANSACTION A...Or it might be another way to perform error handling. I will apresiate your help in "Transaction Error Handling".Thank you in advance." |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-02-22 : 11:46:50
|
| BEGIN TRANSACTION AINSERTUPDATEDELETEIF @@ERROR<>0ROLLBACK TRANSACTION AELSECOMMIT TRANSACTION AThis is incorrect as it does not check for any errors with the update/delete.Your other example will not work either.if @@error <> 0 goto errerr:if @@error <> 0 ** is checking for any errors in the goto statement so will never rollback. You are also commiting after the rollbackJust take out the check and put the commit before itIF @@ERROR<>0 GOTO ERR...IF @@ERROR<>0 GOTO ERRGOTO SUCERR:ROLLBACK TRANSACTION ARETURNSUC:COMMIT TRANSACTION ARETURNor you couldIF @@ERROR<>0 GOTO ERRCOMMIT TRANSACTION ARETURNERR:ROLLBACK TRANSACTION ARETURNYou also might want to save the error code and rowcountupdateselect @error = @@error, @rowcount = @@rowcountif @error <> 0begin raiserror('failed update tbl err = %d', 16, -1, @error) goto errendcommit tranreturnerr:rollback tranreturn==========================================Cursors are useful if you don't know sql.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|