title: SQL Server : Limit rows by variable
body: <p>I have a code that works fine as it is. I Only want to modify one line, but I can't figure out how to do it, or is it even possible.</p>
  
  <p>The code is below.</p>
  
  <pre><code>EXECUTE sp_executesql
  N'SELECT [Brand],
           [month],
           cast(sum([Volume]) AS INT) as [Volume (t)],
           AVG(cast(sum([Volume])AS INT)) OVER (PARTITION BY [Brand]) AS [Average (t)],
  
          CASE WHEN (Row_Number() OVER (ORDER BY [month]))&gt;@v_period
          THEN AVG(cast(sum([Volume])AS INT)) 
                      OVER (PARTITION BY [Brand] ORDER BY [month] 
                      ROWS
                      BETWEEN 3 PRECEDING AND 1 PRECEDING) 
                      ELSE NULL END 
                      AS [Moving Average]
  
  FROM dw_lc_full_aggregated_1
  
  WHERE   [year] = @v_year AND 
          [month] BETWEEN 1 AND 9 AND
          [Navision Source]=@v_navision_source AND
          [entry_type]=@v_entry_type AND
          [Brand]=@v_brand
  
  GROUP BY [Brand],
           [month]',
  
      N'@v_period INT,
        @v_brand varchar (50),
        @v_year INT,
        @v_navision_source varchar (50),
        @v_entry_type varchar (50)',
  
      @v_period=3,
      @v_brand='xxxxxxxxx',
      @v_year=2016,
      @v_navision_source = 'HU',
      @v_entry_type='Country upload'
  </code></pre>
  
  <p>I want to modify this line to use the @v_period variable instead of "3".</p>
  
  <p>BETWEEN 3 PRECEDING AND 1 PRECEDING</p>