Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Error Handling in Transactions
Message
De
27/08/2002 17:27:40
 
 
À
27/08/2002 14:34:38
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Autre
Divers
Thread ID:
00694172
Message ID:
00694256
Vues:
12
If a stored procedure is going to handle transactions locally, it needs to fully handle them. This includes handling error conditions and rolling back the transaction, if necessary. Here's an example:
CREATE PROCEDURE x
AS
SET NOCOUNT ON
DECLARE @err int
SET @err = 0

BEGIN TRANSACTION
UPDATE myFirstTable ...
SET @err = @@ERROR
IF @err != 0 GOTO __cleanup

UPDATE mySecondTable ...
SET @err = @@ERROR
IF @err != 0 GOTO __cleanup

__cleanup:
IF @err != 0 
BEGIN
 IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
END
ELSE 
BEGIN
 IF @@TRANCOUNT > 0 COMMIT TRANSACTION
END
RETURN CASE WHEN @err = 0 THEN 0 ELSE -1 END
If a proc does not initiate a transaction, it should do nothing with them.

This is all just my opinion of course...

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform