Efficient way of getting @@rowcount from a query using row_number

后端 未结 4 1335
-上瘾入骨i
-上瘾入骨i 2020-12-04 08:37

I have an expensive query using the row_number over() functionality in SQL Server 2005. I return only a sub list of those records as the query is paginated. However, I wou

4条回答
  •  隐瞒了意图╮
    2020-12-04 09:24

    Check out the COUNT(*) aggregate when used with OVER(PARTITON BY..), like so:

        SELECT
         ROW_NUMBER() OVER(ORDER BY object_id, column_id) as RowNum
        , COUNT(*) OVER(PARTITION BY 1) as TotalRows
        , * 
        FROM master.sys.columns
    

    This is IMHO the best way to do it without having to do two queries.

提交回复
热议问题