Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 January SQL Server 2012: Custom paging using OFFSET and FETCH NEXT

SQL Server 2012: Custom paging using OFFSET and FETCH NEXT

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demo on SQL Server 2012 Pagination or Custom paging query using OFFSET and FETCH.

Whenever thousands or millions of rows are returning from a database at that time paging is required.
Using custom paging, we can return only limited data for a particular page.

For example, database stored procedure returned 10,00,00 records at one call. How can you arrange this many data into the application grid or any report?

In this kind of requirement, database paging is playing an important role.
The Application has to call a stored procedure using two main parameters:

Page number
Page size

SQL Server 2012 introduced powerful FETCH and OFFSET features.

FETCH and OFFSET retires only small portion of rows from the primary result set.

Using FETCH, you can set a total number of rows.

Using OFFSET, you can skip some rows.

First, create a sample table and insert sample 10000 records.

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_SamplePaging
(
EmpID INT IDENTITY(1,1)
,EmpName VARCHAR(250)
)
GO
 
INSERT INTO tbl_SamplePaging(EmpName)
VALUES ('ABCD' + CAST((SELECT MAX(EmpID) FROM tbl_SamplePaging) AS VARCHAR))
GO 10000

The custom paging query using PageSize and PageNumber parameters:

1
2
3
4
5
6
7
8
9
10
11
12
DECLARE
@PageSize INT = 10,
@PageNumber INT = 1;
 
SELECT
COUNT(1) OVER() AS TotalCount
,EmpID
,EmpName
FROM tbl_SamplePaging
ORDER BY EmpID
OFFSET (@PageNumber-1)*@PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY

The result of first page with 10 records:

SQL Server Custom Paging

Jan 24, 2016Anvesh Patel
SQL Server: Newly created Table with red underline and showing as an Invalid ObjectSQL Server 2012: Introduced dm_db_log_space_usage to find a Log size
Comments: 6
  1. Leeon
    February 29, 2016 at 8:29 am

    Hi, just wanted to mention, I enjoyed this post.
    It was practical. Continue posting!

  2. Dinesh Sharma
    January 26, 2017 at 6:10 pm

    Hi Anvesh,

    I am facing performance bottleneck due to this FETCH NEXT way of pagination as my table contains more than a Billion records. Can you please guide on alternate approach of pagination (fast)?

    Thank You!

    • Anvesh Patel
      Anvesh Patel
      January 27, 2017 at 7:34 pm

      Because of COUNT(1) OVER. Please remove this from my query and try it once again.

  3. DavidMug
    June 27, 2017 at 1:03 am

    great work anvesh! keep it up

  4. Jesse
    August 2, 2017 at 10:36 am

    nice bit of help!

  5. Partik
    July 28, 2018 at 9:43 am

    Yes working, we are using this logic in our portal grid data.

Anvesh Patel
Anvesh Patel

Database Engineer

January 24, 2016 SQL ServerAnvesh Patel, Custom Paging, database, database research and development, dbrnd, limit, offset, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....