Introduction
SQL Server has a new Paging function which is far easier and provides better performance compared to its predecessors. In this article, we will compare the pagination mechanism between previous versions and how it can be done in SQL Server.
This article assumes that SQL Server is installed on the computer to test the query. Open SQL Server Management Studio and create a dummy database to check the new pagination function.

Figure 1. Creating a new database
Name the database as "Dummy" as below.

Figure 2. Naming new database
Click the "Add" button, and it will create a database called "Dummy". Now create a new table in the database by running the following script.
Example
USE [Dummy]
GO
/****** Object: Table [dbo].[DummyTable] Script Date: 10/1/2012 9:00:12 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[DummyTable](
[DummyID] [int] NOT NULL,
[Name] [varchar](50) NULL,
[Details] [varchar](50) NULL,
CONSTRAINT [PK_DummyTable] PRIMARY KEY CLUSTERED
(
[DummyID] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
Now we need to insert some 5000 records into the table to check the pagination function in SQL Server.
Run the following script to do that.
Example
DECLARE @count INT = 1;
DECLARE @max INT = 5000;
DELETE FROM DummyTable;
WHILE (@count <= @max)
BEGIN
INSERT INTO DummyTable (DummyID, Name, Details)
SELECT @count, 'Name' + CAST(@count AS VARCHAR(5)), 'Details' + CAST(@count AS VARCHAR(5));
SET @count = @count + 1;
END
This will insert 5000 records into the table.
Pagination in previous SQL Server versions
Common Practice 1
We normally create a pagination control in the UI and pass a start value and end value to the stored procedure to get the records.
Let us see how we would do that in versions prior.
Example
CREATE PROCEDURE PaginationBefore2012
(
@start INT = 1,
@end INT = 500
)
AS
BEGIN
SELECT
DummyID,
Name,
Details
FROM
DummyTable
WHERE
DummyID BETWEEN @start AND @end
ORDER BY
DummyID;
END
To get records from 1 to 10, we execute the procedure as below.
Pagination Before2012 1,10.







Register RegisterPosted Sep 10, 2021, 11:10 AM
How to get the data of the last page dynamically?
Akshaya arumugamPosted Nov 8, 2018, 11:36 PM
I have 18 lakhs of records i want to paginate it per page contains only 10000 records how to do it efficiently.Will offset,fetch help?
Bhavesh JadavPosted Mar 19, 2018, 7:49 AM
It's helpful to me thanks to share
Upendra Pratap ShahiPosted Jul 14, 2017, 12:56 AM
Nice one...................
Rohatash KumareditedPosted Oct 3, 2012, 11:32 PMEdited Oct 3, 2012, 11:35 PM
Nice Article. Akkiraju Ivaturi. For More info: http://www.c-sharpcorner.com/UploadFile/rohatash/offset-and-fetch-next-keywords-in-sql-server-2012/
Megha GoyalPosted Oct 3, 2012, 11:27 PM
wow its a good concept in SQL Server 2012
Gaurav ChauhanPosted Oct 3, 2012, 11:14 PM
Its a nice function provided in SQL server 2012 i will surely try it....
Varesh TuliPosted Oct 3, 2012, 11:12 PM
Thanks for sharing this knowledgeable article..
lavisha rastogiPosted Oct 3, 2012, 11:06 PM
very nice and knowledgable..........