title: How to pass int parameter in dynamic sql query
body: <p>This is my stored procedure:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[procViewAdvSearchL1]
  -- Add the parameters for the stored procedure here
      @keyword nvarchar(800),
      @CourtID    int = null,
      @SYear int = null,
      @EYear int = null
  
  AS
  BEGIN
  
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;
  
  IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[viewTextSearchL]'))
  DROP VIEW [dbo].[viewTextSearchL]
  
  
  Declare @sqlquery nvarchar(1500)
  Set @sqlquery ='
  CREATE VIEW viewTextSearchL
  AS
      SELECT     tblCase.judgement_id AS ID, tblCase.case_number AS Case_No, tblCase.applicant_name AS Aplicant, tblCase.respondent_name AS Respondent, tblCase.case_date AS Case_Date, 
                    tblCourt.court_name AS Court_Name, tblCase.YearOfCase
  FROM         tblCase INNER JOIN
                    tblCourt ON tblCase.court_id = tblCourt.court_id INNER JOIN
                    tblJudgement ON tblCase.judgement_id = tblJudgement.judgement_id
  WHERE     CONTAINS(tblJudgement.judgement_desc,'+@keyword+') AND (CAST(tblCase.YearOfCase AS int) BETWEEN '+@SYear+' AND  '+@EYear+')' 
  
  EXECUTE(@sqlquery)
  
  END
  </code></pre>
  
  <p>But this gives error </p>
  
  <blockquote>
    <p>Msg 137, Level 15, State 2, Procedure viewTextSearchL, Line 9<br>
    Must declare the scalar variable "@SYear".</p>
  </blockquote>