title: Are you able to pass the 'TOP' number as a parameter to a stored procedure?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6285394/sql-server-use-a-parameter-to-select-the-top-x-of-the-result-set">SQL Server - use a parameter to select the top X of the result set</a>  </p>
  </blockquote>
  
  
  
  <p>My query in my stored procedure looks something like:</p>
  
  <pre><code>select top 9 from my_table;
  </code></pre>
  
  <p>I'd like to edit the stored procedure to dynamically produce the limit from a parameter, however, this does not seem to be working:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[my_stored_procedure]
     @n INT(2)
  AS
  BEGIN
  
  SELECT TOP @n from my_table;
  </code></pre>
  
  <p>Is this doable? Or do I have to do something like:</p>
  
  <pre><code>@n int(2),
  @sql varchar(30)
  
  @sql = 'select top ' + @n '* from my table';
  exec(@sql);
  </code></pre>
  
  <p>Thanks.</p>