title: Must declare scalar variable in sql server dynamic sql
body: <p>I am having problems running sql server stored procedure. I using a dynamic sql.
  I get the error "Must declare the scalar variable "@EmployeeId".</p>
  
  <p><strong>SQL QUERY</strong></p>
  
  <pre><code>ALTER PROCEDURE [dbo].[GetLeaveDays] -- Add the parameters for the stored 
  procedure here
  @LeaveType varchar(5000), 
  @AdminId int,
  @EmployeeId int
  
  AS 
  BEGIN
  
  SET NOCOUNT ON;
  
  DECLARE 
  @queryString nvarchar(MAX);
  
  SET @queryString = 'SELECT ' + @LeaveType + ' FROM CompulsoryLeave WHERE 
  AdminId = @AdminId AND Id=(SELECT LeaveStructureId FROM Employee WHERE 
  Id=@EmployeeId)';
  
  EXECUTE sp_executesql @queryString,
      N'@AdminId int',
      N'@EmployeeId int',
       @AdminId = @AdminId,
       @EmployeeId=@EmployeeId
  
  END
  </code></pre>