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

半腔热情 提交于 2019-12-10 23:49:35

问题


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?


回答1:


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.




回答2:


-- statement here
SELECT @@ROWCOUNT;

You can also get it on every row of the statement, but of course this is a little more expensive, e.g.

SELECT x, y, z, COUNT(*) OVER() FROM ...


来源:https://stackoverflow.com/questions/21416291/how-to-get-total-number-of-rows-in-a-executed-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!