Monday, January 30, 2017

Pagination with OFFSET / FETCH : A better way

CREATE TABLE [dbo].[Customers]
(
  CustomerID bigint IDENTITY(1,1) NOT NULL,
  FirstName varchar(64) NOT NULL,
  LastName varchar(64) NOT NULL,
  EMail varchar(320) NOT NULL,
  Active bit NOT NULL DEFAULT ((1)),
  Created datetime NOT NULL DEFAULT (sysdatetime()),
  Updated datetime NULL,
  CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([CustomerID] ASC)
);
insert into [dbo].[Customers]
(
  FirstName,
  LastName,
  EMail
)
Values('subrat','samal','ss@ss.com')
CREATE PROCEDURE dbo.usp_Test_WithCount
  @PageNumber INT = 1,
  @PageSize   INT = 100
AS
BEGIN
  SET NOCOUNT ON;
 
  ;WITH pg AS
  (
      SELECT CustomerID, 
             t = COUNT(*) OVER()
      FROM dbo.Customers
      ORDER BY CustomerID
      OFFSET @PageSize * (@PageNumber - 1) ROWS
      FETCH NEXT @PageSize ROWS ONLY
  )
  SELECT c.CustomerID, 
         c.FirstName,
         c.LastName,
         c.EMail, 
         c.Active, 
         c.Created, 
         c.Updated, 
         TotalRowCount = t
  FROM dbo.Customers AS c
  INNER JOIN pg ON pg.CustomerID = c.CustomerID
  ORDER BY c.CustomerID
  OPTION (RECOMPILE);
END
CREATE PROCEDURE dbo.usp_Test_WithCount
         @pageSize int,
  @page int,
  @sortBy int,
  @isAsc bit,
  @search varchar(50)
AS
;WITH CTEPG AS
   (
    SELECT [ProductId]
       ,[TotalRows] = COUNT(*) OVER()
    FROM [SubProjSchema].[Products] 
    WHERE ISNULL([IsDeleted], -1) = 0
       AND
       (
       (@Search IS NULL OR CAST(ProductId AS VARCHAR(50)) LIKE '%' + @Search + '%')
      OR (@Search IS NULL OR CAST(ProductName AS VARCHAR(50)) LIKE '%' + @Search + '%')
      OR (@Search IS NULL OR CAST(ISNULL(UnitPrice, 0) AS VARCHAR(50)) LIKE '%' + @Search + '%')
       )
    ORDER BY CASE WHEN @SortBy = 1 AND @IsAsc = 0 THEN [ProductId] END DESC,
       CASE WHEN @SortBy = 1 AND @IsAsc = 1 THEN [ProductId] END ASC,
       CASE WHEN @SortBy = 2 AND @IsAsc = 0 THEN [ProductName] END DESC,
       CASE WHEN @SortBy = 2 AND @IsAsc = 1 THEN [ProductName] END ASC,
       CASE WHEN @SortBy = 3 AND @IsAsc = 0 THEN [UnitPrice] END DESC,
       CASE WHEN @SortBy = 3 AND @IsAsc = 1 THEN [UnitPrice] END ASC
    OFFSET ((@Page - 1)  * @PageSize) ROWS
    FETCH NEXT @PageSize  ROWS ONLY
   )
   SELECT p.[ProductId]
      ,p.[ProductName]
      ,p.[UnitPrice]
      ,CTEPG.[TotalRows]
   FROM [SubProjSchema].[Products] AS p
   INNER JOIN CTEPG ON CTEPG.[ProductId] = p.[ProductId]
   ORDER BY CASE WHEN @SortBy = 1 AND @IsAsc = 0 THEN p.[ProductId] END DESC,
      CASE WHEN @SortBy = 1 AND @IsAsc = 1 THEN p.[ProductId] END ASC,
      CASE WHEN @SortBy = 2 AND @IsAsc = 0 THEN p.[ProductName] END DESC,
      CASE WHEN @SortBy = 2 AND @IsAsc = 1 THEN p.[ProductName] END ASC,
      CASE WHEN @SortBy = 3 AND @IsAsc = 0 THEN p.[UnitPrice] END DESC,
      CASE WHEN @SortBy = 3 AND @IsAsc = 1 THEN p.[UnitPrice] END ASC
   OPTION (RECOMPILE);
http://sqlfidd
le.com/#!6/fd831/1

No comments:

Post a Comment

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...