How to retrieve the total row count of a query with TOP

前端 未结 5 2080
梦如初夏
梦如初夏 2021-01-06 07:22

I have a SQL Server 2008 query

SELECT TOP 10 *
FROM T
WHERE ...
ORDER BY ...

I\'d like to get also the total number of the rows. The obious

5条回答
  •  被撕碎了的回忆
    2021-01-06 08:07

    What is in this answer seems to work:

    https://stackoverflow.com/a/19125458/16241

    Basically you do a:

    SELECT top 100 YourColumns, TotalCount = Count(*) Over()
    From YourTable
    Where SomeValue = 32
    

    TotalCount will have the total number of rows. It is listed on each row though.

    When I tested this the query plan showed the table only being hit once.

提交回复
热议问题