How to get total number of rows in a executed select statement?

count, join, sql, sql-server-2012

Solution

You either have to use `SELECT COUNT(*) ...` with the same condition or add a column with the row-count via `ROW_NUMBER` function:

SELECT a.Emp,b.orders, RN = ROW_NUMBER () OVER (ORDER BY a.Emp,b.orders) 
FROM table as a inner join table1 b on a.ID=B.ID

...or use `@@ROWCOUNT` after the select.

Instead of `ROW_NUMBER` it's easier to use `COUNT(*) OVER ( Order By ...)` where each row contains the same total-count whereas `ROW_NUMBER` would return a sequential number where only the last record(acc. to the `ORDER BY`) would have the total-count.

So what Aaron has already meantioned in his answer.

Problem

How can I out how many rows I obtained after execution? My query is: ``` SELECT a.Emp,b.orders from table as a inner join table1 b on a.ID = B.ID ``` How do I find the number of rows returned in the above join?

Original source

Related problems