Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return total records from SQL Server when using ROW_NUMBER

I would like to return the total number of records in the database so I can set up pagination. How do I return the total number of records in the DB when using the following paging method in SQL Server 2008?

    ALTER PROCEDURE [dbo].[Nop_LoadAllOptimized]     (         @PageSize int = 20,         @PageNumber int = 1,         @WarehouseCombinationID int = 1,         @CategoryId int = 58,         @OrderBy int = 0,         @TotalRecords int = null OUTPUT     )     AS     BEGIN     WITH Paging AS (         SELECT rn = (ROW_NUMBER() OVER (         ORDER BY              CASE WHEN @OrderBy = 0 AND @CategoryID IS NOT NULL AND @CategoryID > 0             THEN pcm.DisplayOrder END ASC,             CASE WHEN @OrderBy = 0             THEN p.[Name] END ASC,             CASE WHEN @OrderBy = 5             THEN p.[Name] END ASC,             CASE WHEN @OrderBy = 10             THEN wpv.Price END ASC,             CASE WHEN @OrderBy = 15             THEN wpv.Price END DESC,             CASE WHEN @OrderBy = 20             THEN wpv.Price END DESC,             CASE WHEN @OrderBy = 25             THEN wpv.UnitPrice END ASC           )), p.*, pcm.DisplayOrder, wpv.Price, wpv.UnitPrice FROM Nop_Product p         INNER JOIN Nop_Product_Category_Mapping pcm ON p.ProductID=pcm.ProductID         INNER JOIN Nop_ProductVariant pv ON p.ProductID = pv.ProductID         INNER JOIN Nop_ProductVariant_Warehouse_Mapping wpv ON pv.ProductVariantID = wpv.ProductVariantID         WHERE pcm.CategoryID = @CategoryId AND (wpv.Published = 1 AND pv.Published = 1 AND p.Published = 1 AND p.Deleted = 0)         AND wpv.WarehouseID IN (select WarehouseID from Nop_WarehouseCombination where UserWarehouseCombinationID = @WarehouseCombinationID)         )     SELECT TOP (@PageSize) * FROM Paging PG     WHERE PG.rn > (@PageNumber * @PageSize) - @PageSize       SET @TotalRecords = @@ROWCOUNT       END 
like image 763
izip Avatar asked Apr 26 '11 13:04

izip


People also ask

What does ROW_NUMBER () over partition by do?

The row rank is calculated using the function ROW_NUMBER(). Let's first use this function and view the row ranks. The ROW_NUMBER() function uses the OVER and PARTITION BY clause and sorts results in ascending or descending order. It starts ranking rows from 1 per the sorting order.

Can we use ROW_NUMBER in SQL Server?

The most commonly used function in SQL Server is the SQL ROW_NUMBER function. The SQL ROW_NUMBER function is available from SQL Server 2005 and later versions. ROW_NUMBER adds a unique incrementing number to the results grid. The order, in which the row numbers are applied, is determined by the ORDER BY expression.

Does Rownum numbers record in a result set?

ROWNUM numbers the records in a result set. The first record that meets the WHERE clause criteria in a SELECT statement is given a row number of 1, and every subsequent record meeting that same criteria increases the row number.


1 Answers

I typically do it this way - never really checked whether it's very efficient from a performance point of view:

WITH YourCTE AS  (    SELECT         (list of columns),        ROW_NUMBER() OVER (ORDER BY ......) AS 'RowNum'     FROM dbo.YourBaseTable ) SELECT      *,     (SELECT MAX(RowNum) FROM YourCTE) AS 'TotalRows'  FROM      YourCTE WHERE        RowNum BETWEEN 101 AND 150 

Basically, the RowNum value will have values 1 through the total of rows (if you don't have a PARTITION BY in your CTE) and thus selecting MAX(RowNum), you get the total number of rows.

like image 68
marc_s Avatar answered Sep 20 '22 15:09

marc_s