Debe declarar el error de la variable escalar en el procedimiento almacenado

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

I have the Following Code in My stored Procedure. i have already declared that @vReturnVar but still showing 'Must Declare the scalar variable "@vReturnVar".

preguntado el 28 de mayo de 14 a las 14:05

What line is your error coming from? Could the variable be out of scope at that point? -

Msg 137, Level 15, State 2, Line 19 Must declare the scalar variable "@vReturnVar". -

I am Just Trying to Run the main Select query to check the result. but the following error is shown. -

If you are just selecting and executing the SELECT query then it has no concept of the variable. The variable must be part of the SQL you are executing. Copy and paste that section into the beginning of your query that you are trying to run. -

You almost certainly want to look at JBrook's answer below too -

3 Respuestas

Shouldn't it include a "COUNT()"

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

contestado el 28 de mayo de 14 a las 14:05

Yes @Jbooks ! i had corrected that error ! that was a mistake . - Santanu Maulik

poner

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

Una vez que el BEGIN of the stored procedure

contestado el 28 de mayo de 14 a las 14:05

Did that but Still Same Problem . I cant understand why is this happening . - Santanu Maulik

declare after begin DECLARE @vReturnVar VARCHAR(10)

surely works for you

contestado el 28 de mayo de 14 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.