title: TSQL: Is there a way to limit the rows returned and count the total that would have been returned without the limit (without adding it to every row)?
body: <p>I'm working to update a stored procedure that current selects up to n rows, if the rows returned = n, does a select count without the limit, and then returns the original select and the total impacted rows.</p>
  
  <p>Kinda like:</p>
  
  <pre><code>SELECT TOP (@rowsToReturn)
      A.data1,
      A.data2
  FROM
      mytable A
  
  SET @maxRows = @@ROWCOUNT
  IF @rowsToReturn = @@ROWCOUNT
  BEGIN
    SET @maxRows = (SELECT COUNT(1) FROM mytableA)
  END    
  </code></pre>
  
  <p>I'm wanting reduce this to a single select statement.  Based on <a href="https://stackoverflow.com/questions/16175955/t-sql-get-row-count-before-top-is-applied">this question</a>, <code>COUNT(*) OVER()</code> allows this, but it is put on every single row instead of in an output parameter.  Maybe something like <code>FOUND_ROWS()</code> in MYSQL, such as a @@TOTALROWCOUNT or such.</p>
  
  <p>As a side note, since the actual select has an order by, the data base will need to already traverse the entire set (to make sure that it gets the correct first n ordered records), so the database should already have this count somewhere.</p>