-- ============================================= -- 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> GOI when tried to execute it and here is what I got: