title: SQL select count of all rows when using IF ELSE
body: <p>I have a situation when I use IF ELSE statement in SQL.</p>
  
  <pre><code>    @searchString nvarchar(50),
      @languageId int, 
      @count int,
      @id int
  IF(@count IS NOT NULL)
      SELECT TOP (@count) Id, value
      FROM TABLE1
          WHERE  (Id IN
          (SELECT Id
      FROM      TABLE2
      WHERE   (Id = @id))) and languageId = @languageId AND value LIKE '%' + @searchString + '%'
  ORDER BY value
  
  ELSE
      SELECT Id, value
      FROM TABLE1
          WHERE  (Id IN
          (SELECT Id
          FROM      TABLE2
      WHERE   (Id = @id))) and languageId = @languageId AND value LIKE '%' + @searchString + '%'
  ORDER BY value
  </code></pre>
  
  <p>I would like to return number of all rows using 
              count(*) over() (or something similar) 
  (as I return only TOP count records for now), like it is answered here:
  <a href="https://stackoverflow.com/questions/5637254/how-to-return-total-number-of-records-with-top-select">How to return total number of records with TOP * select</a></p>
  
  <p>BUT: I wouldn't return this value for every instance, but I would like to return count just once.</p>
  
  <p>Is there a way to do this with one query, or I have to write a separate query for this?</p>
  
  <p>Any hint would be greatly appreciated!</p>
  
  <p>EDIT: using SQL server 2008 r2.</p>