all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

Error Handling in Nested Transactions



Error Handling in Nested Transactions SqlBeginner
11/14/2006 10:28:01 PM
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
Re: Error Handling in Nested Transactions SqlBeginner
11/14/2006 11:25:02 PM
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]
Re: Error Handling in Nested Transactions SqlBeginner
11/14/2006 11:39:02 PM
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]
Re: Error Handling in Nested Transactions Uri Dimant
11/15/2006 12:00:00 AM
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]

Re: Error Handling in Nested Transactions Uri Dimant
11/15/2006 12:00:00 AM
Hi
I'm not sure that understand you, can you elaborate a little bit?



[quoted text, click to view]

Re: Error Handling in Nested Transactions Uri Dimant
11/15/2006 12:00:00 AM
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]

AddThis Social Bookmark Button