Tuesday, April 18, 2017

Paging in SQL Server

It is not a good approach to pull of all the data and show in a page.There are two ways to do paging
1- server side (c# or any other language)
2- Database Side
For this article we will implement in SQL Server 2008 R2.We can make a store procedure and pass two parameters
First  is for where we start from.
Second  is  how much you want to show the record.
I just write the main script you can make a store procedure and pass the parameters

DECLARE  @StartRow INT
DECLARE  @EndRow INT

    SET    @StartRow = 1
    SET @EndRow = 30
   
SELECT    RowNumber as 'S/N',FirstName, LastName, EmailAddress
    FROM (
    SELECT   FirstName, LastName, EmailAddress,
                ROW_NUMBER() OVER(
            ORDER BY FirstName, LastName, EmailAddress,Customerid) AS RowNumber
        FROM    customer) customer
    WHERE    RowNumber > @StartRow
        AND RowNumber < @EndRow
    ORDER BY FirstName, LastName, EmailAddress

No comments:

Post a Comment