Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
First SP
Message
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Titre:
Divers
Thread ID:
00759576
Message ID:
00759668
Vues:
15
>>Sergey,
>>
>>Check, please, Title: Re: SP with multiple tables and databases (insert & delete) Thread #758108 Message #758227
>>
>>Which method is preferable? Should I change the first part this way?
>
>If Mike says something is better than it's better. :)

Ok.

Here is my question: (and I don't understand, how to use Help to find an answer).

In VFP we have:
If
  statement
else
  statement
endif
If we want to exit procedure earlier, we can use return in the middle (though not recommended).

Here is how I'm trying to change my SP, but I don't understand, how can I close:
/*
   If exists, grab CartID and update LastModified field
   otherwise insert a record and grab generated CartID
*/
/* The code bellow belongs to Bob Archer
   See Title: Re: SP with multiple tables and databases 
              (insert & delete) Thread #758108 Message #758212 
   According to Mike Levy another variant (see bellow)
   will be much better on your locks and concurrency

   UPDATE cart
   SET LastModTs = CURRENT_TIMESTAMP,
   @CartID = CartID
   FROM cart
   WHERE UserID = @tcUserID

   IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN
            PRINT 'ERROR: An error occurred while attempting to update the Cart Table.'
            RETURN 3
        END

    IF @@RowCount = 0 --Record doesn't exist
        BEGIN
	   INSERT INTO cart (UserID) values (@tcUserID)

	   SELECT @CartID = SCOPE_IDENTITY()
        END
*/

-- Code from Mike Levy 
IF EXISTS (SELECT CartID FROM cart WHERE UserID = @tcUserID)
   UPDATE cart
   SET LastModTs = CURRENT_TIMESTAMP,
   @CartID = CartID
   FROM cart
   WHERE UserID = @tcUserID
ELSE
        BEGIN
	   INSERT INTO cart (UserID) values (@tcUserID)

	   SELECT @CartID = SCOPE_IDENTITY()
        END

     IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN
            PRINT 'ERROR: An error occurred while attempting to insert record to the Cart Table.'
            RETURN 4
        END
Basically, I want to check, if my insert was correct or my update was correct. In both cases, if something got wrong, I need to rollback and get out from procedure.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform