sql server programming:
Hi All, I want to know the best practise (oops in the first place a solution then the best practise :)) for error handling in nested transactions. That is, let me explain with an example: 1. From Stored Procedure 1 i would be calling SP2 2. From SP2 ... SP3 would be called. I want all these 3 SP's to execute in a transaction. Even if it fails in one SP what ever happened in other SPs should be rolled back. I understand we need to make user of Begin Trans, Commit Tran and Rollback Tran. But I don't know how to capture this error and rollback. Can somebody provide me with an example! Regards Pradeep
Thanks for the response Uri. Can you tell me how should i code sp1 to sp4. i.e., how to return the error code from there? Regards Pradeep [quoted text, click to view] "Uri Dimant" wrote: > Hi > CREATE PROCEDURE BigOne > AS > DECLARE @err integer > BEGIN TRANSACTION > EXEC @err = sp1 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp2 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp3 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp4 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > COMMIT TRANSACION > GO > > > "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message > news:81F0BF37-E3A2-4556-9930-0858FBD64444@microsoft.com... > > Hi All, > > > > I want to know the best practise (oops in the first place a solution then > > the best practise :)) for error handling in nested transactions. > > > > That is, let me explain with an example: > > 1. From Stored Procedure 1 i would be calling SP2 > > 2. From SP2 ... SP3 would be called. > > > > I want all these 3 SP's to execute in a transaction. Even if it fails in > > one > > SP what ever happened in other SPs should be rolled back. > > > > I understand we need to make user of Begin Trans, Commit Tran and > > Rollback > > Tran. But I don't know how to capture this error and rollback. Can > > somebody > > provide me with an example! > > > > Regards > > Pradeep > > > >
Also another question: What if one of the nested Sp can work on it own (for someother situation). So if I have mentioned begin / Commit / Rollback tran within it (for ex: SP2). How would this error handling work. Hope i have made my question clear! Regards Pradeep [quoted text, click to view] "Uri Dimant" wrote: > Hi > CREATE PROCEDURE BigOne > AS > DECLARE @err integer > BEGIN TRANSACTION > EXEC @err = sp1 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp2 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp3 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > EXEC @err = sp4 > SELECT @err = coalesce(nullif(@err, 0, @@error) > IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END > COMMIT TRANSACION > GO > > > "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message > news:81F0BF37-E3A2-4556-9930-0858FBD64444@microsoft.com... > > Hi All, > > > > I want to know the best practise (oops in the first place a solution then > > the best practise :)) for error handling in nested transactions. > > > > That is, let me explain with an example: > > 1. From Stored Procedure 1 i would be calling SP2 > > 2. From SP2 ... SP3 would be called. > > > > I want all these 3 SP's to execute in a transaction. Even if it fails in > > one > > SP what ever happened in other SPs should be rolled back. > > > > I understand we need to make user of Begin Trans, Commit Tran and > > Rollback > > Tran. But I don't know how to capture this error and rollback. Can > > somebody > > provide me with an example! > > > > Regards > > Pradeep > > > >
Hi CREATE PROCEDURE sp1 AS BEGIN TRANSACTION UPDATE Table1 … SET @Err = @@ERROR IF @Err <> 0 BEGIN ROLLBACK TRANSACTION RETURN (@Err) END COMMIT TRANSACTION [quoted text, click to view] "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message news:E2355AF8-E4DF-4B36-A55C-09A01649C336@microsoft.com... > Thanks for the response Uri. Can you tell me how should i code sp1 to sp4. > i.e., how to return the error code from there? > > Regards > Pradeep > > "Uri Dimant" wrote: > >> Hi >> CREATE PROCEDURE BigOne >> AS >> DECLARE @err integer >> BEGIN TRANSACTION >> EXEC @err = sp1 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp2 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp3 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp4 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> COMMIT TRANSACION >> GO >> >> >> "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message >> news:81F0BF37-E3A2-4556-9930-0858FBD64444@microsoft.com... >> > Hi All, >> > >> > I want to know the best practise (oops in the first place a solution >> > then >> > the best practise :)) for error handling in nested transactions. >> > >> > That is, let me explain with an example: >> > 1. From Stored Procedure 1 i would be calling SP2 >> > 2. From SP2 ... SP3 would be called. >> > >> > I want all these 3 SP's to execute in a transaction. Even if it fails >> > in >> > one >> > SP what ever happened in other SPs should be rolled back. >> > >> > I understand we need to make user of Begin Trans, Commit Tran and >> > Rollback >> > Tran. But I don't know how to capture this error and rollback. Can >> > somebody >> > provide me with an example! >> > >> > Regards >> > Pradeep >> > >> >> >>
Hi I'm not sure that understand you, can you elaborate a little bit? [quoted text, click to view] "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message news:28C4214F-6149-4609-BCF9-355374AA86DB@microsoft.com... > Also another question: > > What if one of the nested Sp can work on it own (for someother situation). > So if I have mentioned begin / Commit / Rollback tran within it (for ex: > SP2). How would this error handling work. > > Hope i have made my question clear! > > Regards > Pradeep > > "Uri Dimant" wrote: > >> Hi >> CREATE PROCEDURE BigOne >> AS >> DECLARE @err integer >> BEGIN TRANSACTION >> EXEC @err = sp1 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp2 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp3 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> EXEC @err = sp4 >> SELECT @err = coalesce(nullif(@err, 0, @@error) >> IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END >> COMMIT TRANSACION >> GO >> >> >> "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message >> news:81F0BF37-E3A2-4556-9930-0858FBD64444@microsoft.com... >> > Hi All, >> > >> > I want to know the best practise (oops in the first place a solution >> > then >> > the best practise :)) for error handling in nested transactions. >> > >> > That is, let me explain with an example: >> > 1. From Stored Procedure 1 i would be calling SP2 >> > 2. From SP2 ... SP3 would be called. >> > >> > I want all these 3 SP's to execute in a transaction. Even if it fails >> > in >> > one >> > SP what ever happened in other SPs should be rolled back. >> > >> > I understand we need to make user of Begin Trans, Commit Tran and >> > Rollback >> > Tran. But I don't know how to capture this error and rollback. Can >> > somebody >> > provide me with an example! >> > >> > Regards >> > Pradeep >> > >> >> >>
Hi CREATE PROCEDURE BigOne AS DECLARE @err integer BEGIN TRANSACTION EXEC @err = sp1 SELECT @err = coalesce(nullif(@err, 0, @@error) IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END EXEC @err = sp2 SELECT @err = coalesce(nullif(@err, 0, @@error) IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END EXEC @err = sp3 SELECT @err = coalesce(nullif(@err, 0, @@error) IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END EXEC @err = sp4 SELECT @err = coalesce(nullif(@err, 0, @@error) IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN 1 END COMMIT TRANSACION GO [quoted text, click to view] "SqlBeginner" <SqlBeginner@discussions.microsoft.com> wrote in message news:81F0BF37-E3A2-4556-9930-0858FBD64444@microsoft.com... > Hi All, > > I want to know the best practise (oops in the first place a solution then > the best practise :)) for error handling in nested transactions. > > That is, let me explain with an example: > 1. From Stored Procedure 1 i would be calling SP2 > 2. From SP2 ... SP3 would be called. > > I want all these 3 SP's to execute in a transaction. Even if it fails in > one > SP what ever happened in other SPs should be rolled back. > > I understand we need to make user of Begin Trans, Commit Tran and > Rollback > Tran. But I don't know how to capture this error and rollback. Can > somebody > provide me with an example! > > Regards > Pradeep >
Don't see what you're looking for? Try a search.
|