1

I have a big select with many inner join and left join and select to select like:

SELECT 
    ( ... ) 
ORDER BY 
    Price 

The question is for count of select am I must to run this select again ?

SELECT    
    COUNT( ... )  
ORDER BY 
    Price 

Is there any easy way to run one times and get result of select and count of select ?

Here is my C# code with Entity Framework:

string strQuery = "....";

IQueryable<ProductDto> list = _entities.Database.SqlQuery<ProductDto>(strQuery).AsQueryable();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dan
  • 51
  • 4

2 Answers2

3

You can use select @@rowcount to get the number of rows selected by the previous statement as a second resultset, or use SET @myOutputVar = @@ROWCOUNT it as an output parameter of your stored procedure.

A few Q/A's here on StackOverflow that may also help

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Can you show it on my query please , I didn't understand it – dan Jul 25 '16 at 08:52
  • @dan - how do you execute your `select` is it a stored procedure? Is it called from .NET code? – Jamiec Jul 25 '16 at 08:53
  • @dan - have you seen this: [`CountAsync()`](https://msdn.microsoft.com/en-us/library/dn177554(v=vs.113).aspx#M:System.Data.Entity.Infrastructure.DbRawSqlQuery`1.CountAsync) – Jamiec Jul 25 '16 at 09:07
0

Yes you can try select @@rowcount.It Returns the number of rows affected by the last statement. If the number of rows is more than 2 billion, use ROWCOUNT_BIG.

https://msdn.microsoft.com/en-IN/library/ms187316.aspx

Manish Kumar
  • 509
  • 5
  • 15