Search code examples
sql-serverstored-procedurestable-valued-parameters

not able to declare variable as TVP type in Stored Proc


I have created a TVP and I am trying to use it in a stored proc for my input into stored proc. the issue is I am not able to create my SP with it. Its says

Msg 137, Level 16, State 1, Procedure uspGetUsersPresentCount, Line 14
Must declare the scalar variable "@usersList".

My SP is

IF EXISTS (SELECT * FROM sysobjects WHERE name = 'uspGetUsersPresentCount')
    BEGIN
        DROP  Procedure  [AMProcedures].[uspGetUsersPresentCount]
    END

GO


SET ANSI_NULLS ON
GO 

SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [AMProcedures].[uspGetUsersPresentCount]
(
    @usersList AS [AMProcedures].[udfUserListTVP] READONLY,
    @startDate DATETIME,
    @endDate DATETIME
)
AS
BEGIN
DECLARE @okStatus TINYINT = 4
SET NOCOUNT ON

    SELECT MIMO.UserID, COUNT(MIMO.MoveInTime) FROM AMTables.tblUserMoveInMoveOutDetails MIMO
        JOIN @usersList ON MIMO.UserID=@usersList.UserID
            WHERE MIMO.Status=@okStatus AND
                MIMO.MoveInTime BETWEEN @startDate AND @endDate
            GROUP BY MIMO.UserID

SET NOCOUNT OFF

END
GO

can anyone tell me what wrong am I doing ...I have tried what I knew, but nothing seems to work.

Thanks in advanced.


Solution

  • Either use an alias for the user list, or put it in square braces:

    JOIN @usersList UL ON MIMO.UserID=UL.UserID
    

    or

    JOIN @usersList ON MIMO.UserID=[@usersList].UserID