Welcome Mohlala
Welcome Mohlala

Reputation: 111

Getting 'Must declare the scalar variable' errors when running stored proc

USE db_preprac_v1_2
GO

CREATE PROCEDURE sp_accessory
@repID AS SMALLINT
AS

--Create a temp copy of table

    SELECT * 
    INTO #tempAcc
    FROM accessory 
    GO

--Add a column called Printed

    ALTER TABLE #tempAcc
    ADD
    Printed SMALLINT
    GO

--Set all the values in that column equal to 0

    UPDATE #tempAcc
    SET Printed = 0
    GO


    --Declare variables

    DECLARE @RepName AS VARCHAR(30)
    DECLARE @RepTel AS VARCHAR(10)
    DECLARE @AccID AS SMALLINT
    DECLARE @AccDesc AS VARCHAR(30)
    DECLARE @AccPrice AS MONEY
    DECLARE @Quantity AS SMALLINT
    DECLARE @total AS MONEY = 0
    --DECLARE @ID AS SMALLINT = @repID

/* Checks if representative id is valid and if valid, it displays all the accessories that the representative sold. If not valid, it prints a error message. */

IF NOT EXISTS(SELECT rep_id FROM representative WHERE rep_id = @repID)
>BEGIN
    PRINT 'Invalid representative id'
END
ELSE
BEGIN
    SELECT @RepName = representative.rep_name, @RepTel = representative.rep_tel
    FROM representative
    WHERE @repID = representative.rep_id

    PRINT '************************'
    PRINT 'Accessory Details Report'
    PRINT '************************'
    PRINT ''
    PRINT 'Representative Information'
    PRINT ''
    PRINT 'Reps Name: ' + @RepName
    PRINT 'Reps Telephone: ' + @RepTel
    PRINT ''

    PRINT 'Accessories Sold by this Representative'

--Creates a while loop to iterate through the rows

WHILE EXISTS (SELECT *
  FROM #tempAcc
  WHERE Printed = 0) 

  BEGIN 
    SELECT @AccID = MIN (acc_id)
    FROM #tempAcc
    WHERE Printed = 0

    SELECT @AccDesc = accessory.acc_desc, @AccPrice = accessory.acc_price, @Quantity = accessory_detail.quantity
    FROM accessory
    JOIN accessory_detail
    ON accessory.acc_id = accessory_detail.acc
    JOIN representative
    ON accessory_detail.rep = representative.rep_id
    WHERE @repID = representative.rep_id

    PRINT 'Accessory Desc: ' + @AccDesc
    PRINT 'Accessory Price: ' + CAST(@AccPrice AS VARCHAR)
    PRINT 'Quantity in Car: ' + CAST(@Quantity AS VARCHAR)
    PRINT ''

    UPDATE #tempAcc
       SET Printed = 1
       WHERE @AccID = acc_id

--Calculates the total value of accesories sold

    SELECT @total = @total + @AccPrice
  END
END

    PRINT 'The total value of the accessories sold is: ' + CAST(@total AS VARCHAR)
    PRINT 'Transaction Date: ' + CAST(GETDATE() AS VARCHAR)     

GO

--to execute the procedure, supply an input value of the representative id.

EXEC sp_accessory 3
GO

DROP PROCEDURE sp_accessory
GO

This is what i get when i execute the code using Microsoft SQL Server Management Studio:

Msg 137, Level 15, State 2, Line 15
Must declare the scalar variable "@repID".
Msg 156, Level 15, State 1, Line 19
Incorrect syntax near the keyword 'ELSE'.
Msg 137, Level 15, State 2, Line 23
Must declare the scalar variable "@repID".
Msg 137, Level 15, State 2, Line 53
Must declare the scalar variable "@repID".

--------------------------------------------------------------------------**

Upvotes: 0

Views: 11789

Answers (1)

Adam V
Adam V

Reputation: 6356

1) Don't name any procedure "sp_(ANYTHING)"; "sp_" should be used for system stored procedures.

2) You have three GO statements in the middle of your procedure. If you run this as a CREATE script, it's going to assume that the first GO marks the end of the script.

3) You have a stray > before your BEGIN after your IF.

4) Why are you dropping the procedure after you execute it?

5) You're not multiplying the Quantity by the price to get the per-line subtotal. The total is therefore incorrect. Change this:

SELECT @total = @total + @AccPrice

to this:

SELECT @total = @total + (@Quantity * @AccPrice)

Upvotes: 2

Related Questions