How to include the total number of returned rows in the resultset from SELECT T-SQL command?

前端 未结 7 1556
一整个雨季
一整个雨季 2020-12-09 09:29

I would like to ask if there is a way to include the total number of rows, as an additional column, in the returned result sets from a TSQL query using also the Row_Nu

7条回答
  •  甜味超标
    2020-12-09 09:45

    In SQL Server 2008 and later, add COUNT(*) OVER () as one of the column names in your query and that will be populated with the total rows returned. It is repeated in every single row but at least the value is available. The reason why many other solutions do not work is that, for very large result sets, you will not know the total until after iterating all rows which is not practical in many cases (especially sequential processing solutions). This technique gives you the total count after calling the first IDataReader.Read(), for instance.

    select COUNT(*) OVER () as Total_Rows, ... from ...
    

提交回复
热议问题