Debe declarar el error de la variable escalar en el procedimiento almacenado
Frecuentes
Visto 3,675 equipos
0
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".
3 Respuestas
1
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
1
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
0
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 sql sql-server-2008 or haz tu propia pregunta.
What line is your error coming from? Could the variable be out of scope at that point? - Ian R. O'Brien
Msg 137, Level 15, State 2, Line 19 Must declare the scalar variable "@vReturnVar". - Santanu Maulik
I am Just Trying to Run the main Select query to check the result. but the following error is shown. - Santanu Maulik
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. - Ian R. O'Brien
You almost certainly want to look at JBrook's answer below too - OGHaza