Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
First SP
Message
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Titre:
First SP
Divers
Thread ID:
00759576
Message ID:
00759576
Vues:
46
Hi everybody,

I'm trying to write an SP. Here is what I've wrote so far:
-- =============================================
-- Create procedure with OUTPUT Parameters
-- =============================================
-- creating the store procedure
IF EXISTS (SELECT name 
	   FROM   sysobjects 
	   WHERE  name = N'sp_InsertShoppingCartInfo' 
	   AND 	  type = 'P')
    DROP PROCEDURE sp_InsertShoppingCartInfo 
GO

CREATE PROCEDURE sp_InsertShoppingCartInfo(
	@tcUserID  char(20),
        @tnNumOfRecords int,
        @tcDescription 	text,
        @tcCreditCategory char(1),
	@tcSelectionCriteria text,
        @tcListOfCodes varchar(500))
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

    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

    DECLARE @CartID  int
    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
	   @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
-- =============================================
-- example to execute the store procedure
-- =============================================
DECLARE <@variable_for_output_parameter, sysname, @p2_output> <datatype_for_output_parameter, , int>
EXECUTE <procedure_name, sysname, proc_test> <value_for_param1, , 1>, <@variable_for_output_parameter, sysname, @p2_output> OUTPUT
SELECT <@variable_for_output_parameter, sysname, @p2_output>
GO
I when tried to execute it and here is what I got:
=======================================================
Server: Msg 170, Level 15, State 1, Procedure sp_InsertShoppingCartInfo, Line 58
Line 58: Incorrect syntax near 'UserID'.
Server: Msg 170, Level 15, State 1, Procedure sp_InsertShoppingCartInfo, Line 70
Line 70: Incorrect syntax near 'CartID'.

What is incorrect here?
If it's not broken, fix it until it is.


My Blog
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform