title: SQL Server Stored Procedure - Use Row Count in Select query
body: <p>My stored procedure (SQL Server 2005) returns a dataset where one field depends, among other things, on the number of rows returned by the query. I can make a simplified first query that allows me to get @@ROWCOUNT but, in that case, the procedure returns the two sets, which is not what I want. </p>
  
  <p>I tried putting the first query in a WITH statement but haven't found the syntax to extract the row count and put it in a variable that I could use in the second query. An alternative would be to get @@ROWCOUNT from the first query and tell the procedure to return only the result of the second query.</p>
  
  <p>There are probably better ways to do that but my expertise in SQL is quite limited...</p>
  
  <p>Thanks for any help!</p>