必须在存储过程中声明标量变量错误

时间:2014-05-28 13:20:29

标签: sql sql-server-2008

ALTER PROCEDURE [dbo].[SP_GetLatestMmdData]

AS
DECLARE
     @vReturnVar VARCHAR(10) 
    ,@vRCount VARCHAR(10)
BEGIN

SET NOCOUNT ON;
SET @vRCount =(SELECT Distinct Shift From [dbo].[tblMMDLogEntry] Where Shift Like 'Night     ' And Date=(SELECT  MAX(Date)  
                FROM [dbo].[tblMMDLogEntry]))
IF(@vRCount>0) 
BEGIN
    SET @vReturnVar='Night      '
END
Else
BEGIN
    SET @vReturnVar='Evening    '
END             

SELECT
    convert(varchar,[Date],103) Date
                    ,Shift
                    ,SlNo
                    ,TotalManpower
                    ,JobDescription
                    ,PermitNo
                    ,StartTime
                    ,EndTime
                    ,AllotedManpower
                    ,Supervisior
                    ,AShift
                    ,GShift
                    ,BShift
                    ,CShift
                    ,Remarks 
    FROM [dbo].[tblMMDLogEntry] WHERE Date=(SELECT  MAX(Date)  
     FROM [dbo].[tblMMDLogEntry]) 
     AND Shift= @vReturnVar 

END

我的存储过程中有以下代码。我已经宣布了@vReturnVar 但仍然显示'必须声明标量变量" @ vReturnVar"。

3 个答案:

答案 0 :(得分:1)

DECLARE
@vReturnVar VARCHAR(10) 
,@vRCount VARCHAR(10)

存储过程的BEGIN之后

答案 1 :(得分:1)

不应该包含“COUNT()”

        SET @vRCount =(SELECT COUNT(Distinct Shift) 
                       From [dbo].[tblMMDLogEntry] 
                       Where Shift Like 'Night     ' And Date=(SELECT  MAX(Date)  
                       FROM [dbo].[tblMMDLogEntry]))

答案 2 :(得分:0)

开始后宣布 DECLARE @vReturnVar VARCHAR(10)

肯定适合你