Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SP with multiple tables and databases (insert & delete)
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00758108
Message ID:
00759675
Views:
17
>>Why your suggestion is better? If UserID exists, we would need to modify, so we don't have to search?
>
>Remember that INSERT, UPDATE, and DELETEs are two step commands. In the first step, SQL Server must find the applicable row or in the case of INSERT, it must find the location to put the new row. In performing this step, SQL Server uses a special lock - UPDATE. It is different than a SHARED lock. It affects concurrency more than a SHARED lock but less than EXCLUSIVE.
>
>> UPDATE cart
>> SET LastModTs = CURRENT_TIMESTAMP,
>> @CartID = CartID
>> FROM cart
>> WHERE UserID = @tcUserID
>>
>>Incorrect syntax near UserID
>
>You don't need to use this form of UPDATE. Try this
>
>UPDATE cart
> SET @cartID = cartID
> ,lastModified = CURRENT_TIMESTAMP
>WHERE
> userid = @tnUserID
>
>Now, if lastModified is defined with the TIMESTAMP datatype, you don't have to explicitly update it. SQL Server will automatically do it.
>
>>Also am I getting right, that you don't have to close IF statements, e.g. is this valid:
>>Does END means an early exit point from the procedure?
>
>BEGIN and END are use to group statements into blocks. TSQL is similar to C, C++, and C# in that conditional and looping statements only affect the statement that appear directly after it. Executing the following:
>
>IF (1=0)
> PRINT 'Hello'
> PRINT 'World'
>
>Will produce "World" since the first PRINT is skipped. But, wrapping BEGIN and END around the two prints will produce the output as you might expect:
>
>IF (1=0)
> BEGIN
> PRINT 'Hello'
> PRINT 'World'
> END
>
>Nothing will be displayed when the above snippet of code is executed.
>
>-Mike

Now I understand. That's great, that and how easily you explained it to me.

Thanks a lot.
So, it would be:
-- Code from Mike Levy 
IF EXISTS (SELECT CartID FROM cart WHERE UserID = @tcUserID)
   BEGIN
   	UPDATE cart
   	SET @CartID = CartID, 
   	LastModTs = CURRENT_TIMESTAMP   
   	WHERE UserID = @tcUserID
   	IF @@ERROR <> 0
          BEGIN
            ROLLBACK TRAN
            PRINT 'ERROR: An error occurred while attempting to update record to the Cart Table.'
            RETURN 4
        END
   END
ELSE 
-- New User
    BEGIN
	 INSERT INTO cart (UserID) values (@tcUserID)

	 SELECT @CartID = SCOPE_IDENTITY()
         IF @@ERROR <> 0
          BEGIN
            ROLLBACK TRAN
            PRINT 'ERROR: An error occurred while attempting to insert record to the Cart Table.'
            RETURN 5
          END
     END
?

How can I highlight a block of code and ident it correctly?

Or should I use one place to handle errors, e.g. like:
USE pubs
GO
DECLARE @del_error int, @ins_error int
-- Start a transaction.
BEGIN TRAN

-- Execute the DELETE statement.
DELETE authors
WHERE au_id = '409-56-7088'

-- Set a variable to the error value for 
-- the DELETE statement.
SELECT @del_error = @@ERROR

-- Execute the INSERT statement.
INSERT authors
   VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
   '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
-- Set a variable to the error value for 
-- the INSERT statement.
SELECT @ins_error = @@ERROR

-- Test the error values.
IF @del_error = 0 AND @ins_error = 0
BEGIN
   -- Success. Commit the transaction.
   PRINT "The author information has been replaced"    
   COMMIT TRAN
END
ELSE
BEGIN
   -- An error occurred. Indicate which operation(s) failed
   -- and roll back the transaction.
   IF @del_error <> 0 
      PRINT "An error occurred during execution of the DELETE 
      statement." 

   IF @ins_error <> 0
      PRINT "An error occurred during execution of the INSERT 
      statement." 

   ROLLBACK TRAN
END
This one is probably preffered method, right?
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform