在运行存储过程时获取“必须声明标量变量”错误

时间:2012-03-05 21:04:56

标签: sql-server-2008

USE db_preprac_v1_2
GO

CREATE PROCEDURE sp_accessory
@repID AS SMALLINT
AS

- 创建表的临时副本

    SELECT * 
    INTO #tempAcc
    FROM accessory 
    GO

- 添加名为Printed

的列
    ALTER TABLE #tempAcc
    ADD
    Printed SMALLINT
    GO

- 将该列中的所有值设置为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

/ *     检查代表ID是否有效,如果有效,则显示所有附件     该代表出售。如果无效,则会输出错误消息。     * /

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'

- 创建一个while循环来遍历行

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

- 计算出售配件的总价值

    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

- 要执行该过程,请提供代表ID的输入值。

EXEC sp_accessory 3
GO

DROP PROCEDURE sp_accessory
GO

这是我使用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".

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

1 个答案:

答案 0 :(得分:2)

1)不要将任何程序命名为“sp_(ANYTHING)”; “sp_”应该用于系统存储过程。

2)您在程序中间有三个GO语句。如果您将其作为CREATE脚本运行,则会假定第一个GO标记脚本的结尾。

3)你有一个迷路>在BEGIN之后的IF之前。

4)执行后为什么要删除程序?

5)您没有将数量乘以价格来获得每行小计。因此总数不正确。改变这个:

SELECT @total = @total + @AccPrice

到此:

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