I'm having an issue using parameters in a dynamic sql stored procedure. I receive "Must declare the scalar variable "@StartIdex"." when I execute the stored procedure. The issue is being caused by the two integer parameters that are being referenced in dynamic sql. Can someone help me resolve this issue ?
@Empid nvarchar(50) = null, @FirstName nvarchar(80) = null, @LastName nvarchar(80) = null, @StartIndex int, @MaximumRows int AS BEGIN SET NOCOUNT ON; Declare @SQLQuery nvarchar(4000) SET @Query = ' Select RowNum,Empid,firstname,lastname From (SELECT ROW_NUMBER() OVER(ORDER BY empid) as RowNum, empid, firstname, lastname, WHERE firstname is not null ) as employee WHERE RowNum BETWEEN @StartIndex AND (@StartIndex + @MaximumRows) - 1' If @Empid is not null Set @Query = @Query + ' And (Empid = @Empid)' If @FirstName is not null Set @Query = @Query + ' And (FirstName like @FirstName + ''%'')' If @LastName is not null Set @Query = @Query + ' And (LastName like @LastName + ''%'')' Execute sp_Executesql @Query