Microsoft SQL.Procedure错误消息137,必须声明标量变量

时间:2015-08-20 11:45:01

标签: sql-server

我仍然是学生程序员,我遇到了这个错误,我一直在努力修复它,无论我尝试什么,我似乎无法找到解决方案,请你帮助我。

我把打印声明留了出来。

CREATE PROCEDURE sp_Report
@CompanyID SMALLINT
AS
DECLARE @companyName VARCHAR(30)
DECLARE @companyNumber VARCHAR (13)
DECLARE @companyEmail VARCHAR(50)
DECLARE @food_ID SMALLINT
DECLARE @food_type VARCHAR(30)
DECLARE @food_name VARCHAR(30)
DECLARE @expiry_Date DATE


SELECT  @companyName = companyName,@companyNumber =    companyNumber,@companyEmail = companyEmail
FROM supplier
WHERE companyID = @CompanyID
SET DATEFORMAT mdy

--Create temp table

SELECT *
INTO #tempSupp
FROM vw_ExpiredFoodDetails
WHERE [Company Name] = @companyName

-- add the printed column into the temporal table
ALTER TABLE #tempSupp
ADD
Printed SMALLINT

--set all values in the printed column to 0
UPDATE #tempSupp
SET Printed = 0

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

BEGIN
    SELECT @companyName = [Company Name]
    FROM #tempSupp
    WHERE Printed = 0 
    SELECT @companyEmail = #tempSupp.companyEmail,
    @companyNumber = #tempSupp.ComNum,
    @companyID = #tempSupp.ComID
    FROM #tempSupp
    Join vw_ExpiredFoodDetails
    ON #tempSupp.ComName = [Company Name]
    WHERE @CompanyID = #temp.ComID

    UPDATE #tempSupp
    SET Printed = 1 
    WHERE @companyName = [Company Name]
END
GO
DROP TABLE #tempSupp
GO

1 个答案:

答案 0 :(得分:0)

    SELECT @companyName = [Company Name]
    FROM #tempSupp
    WHERE Printed = 0
    SELECT @companyEmail = #tempSupp.companyEmail,
    @companyNumber = #tempSupp.ComNum,
    @companyID = #tempSupp.ComID
    FROM #tempSupp
    Join vw_ExpiredFoodDetails
    ON #tempSupp.ComName = [Company Name]
    WHERE @CompanyID = #temp.ComID
                       ^^^^^   

在最后一行,您使用未在任何地方声明的#temp表名。 此外,不需要删除在存储过程中创建的临时表。