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

前端 未结 5 2122
梦如初夏
梦如初夏 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:00

    No.

    SQL Server doesn't keep COUNT(*) in metadata like MyISAM, it calculates it every time.

    UPDATE: If you need an estimate, you can use statistics metadata:

    SELECT  rows
    FROM    dbo.sysindexes
    WHERE   name = @primary_key,
    

    where @primary_key is your table's primary key name.

    This will return the COUNT(*) from last statistics update.

提交回复
热议问题