Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
First SP
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Miscellaneous
Thread ID:
00759576
Message ID:
00759605
Views:
15
>>>First, there's garbage at the end of your sproc. Second, double click on the error message and QA will bring you to the line with error.
>>Why SELECT @CartID = @@SCOPE_IDENTITY gives
>>
>>Server: Msg 137, Level 15, State 2, Procedure sp_InsertShoppingCartInfo, Line 60
>>Must declare the variable '@@SCOPE_IDENTITY'.
>>
>>My variable is declared at the top with DECLARE @CartID int
>
>
SELECT @CartID = SCOPE_IDENTITY()
You should read BOL not just glance over.

If I would know, what to look. But thanks, I've read the difference. That was code from Bob, I assumed, it was correct.

Could you please help me to understand this one (and hopefully that would be it):

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

Can I get CartID while doing update? I decided, that it would be my output parameter, since I would need to get CartID anyway...

I got the procedure compiled OK:
CREATE PROCEDURE sp_InsertShoppingCartInfo(
	@tcUserID  char(20),
        @tnNumOfRecords int,
        @tcDescription 	text,
        @tcCreditCategory char(1),
	@tcSelectionCriteria text,
        @tcListOfCodes varchar(500),
        @tcCardID int OUTPUT)
AS 
        
--	<@param2, sysname, @p2> <datatype_for_param2, , int>  OUTPUT
-- SELECT @p2 = @p2 + @p1

/*
CREATED BY:    Nadya Nosonovsky
CREATED ON:    28/2/2003
PURPOSE:    This routine inserts records into Cart, CartContent and CartDetail tables
            It passes UserID, Description, SelectionCriteria, NumberOfRecords, 
            and list of CredID separated by comma to insert into CartDetail table
*/
-- First do basic validation
BEGIN
    DECLARE @CartID  int

    IF @tcUserID IS NULL
      BEGIN
        PRINT 'ERROR: The UserID value must contain a value.'
        RETURN 1
      END

    IF NOT EXISTS 
         (SELECT * FROM INFORMATION_SCHEMA.TABLES 
          WHERE TABLE_NAME = 'Cart')
        BEGIN  
          PRINT 'ERROR: The Cart table does not exist.'
          RETURN 2 
        END

    BEGIN TRANSACTION
/*
   If exists, grab CartID and update LastModified field
   otherwise insert a record and grab generated CartID
*/
   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

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

     INSERT INTO CartContent 
            (CartID, NumOfRecords, 
            CreditCategory, DescriptionText, SelectionCriteria)
            values (@CartID, @tnNumOfRecords, @tcCreditCategory, 
                    @tcDescription, @tcSelectionCriteria)
    
     IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN
            PRINT 'ERROR: An error occurred while attempting to insert record to the CartContent Table.'
            RETURN 4
        END
          
     commit transaction
END
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