SQLUSA
SQL 2008 GRAND SLAM
FREE TRIAL
CLICK HERE TO ORDER

SQL Server 2008 Best Practices
SQL Server 2005 Best Practices

How to paginate company contact information?

Execute the following SQL Server Transact-SQL script in SSMS Query Editor to create pagination for the Person.Contact table info by applying the ROW_NUMBER() function.

-- SQL Server stored procedure - SQL pagination - SQL row_number function

CREATE PROCEDURE uspContactInfoByPage

                @PageSize   INT,

                @PageNumber INT

AS

  BEGIN

    WITH cteContact

         AS (SELECT ContactID,

                    FirstName,

                    MiddleName,

                    LastName,

                    EmailAddress,

                    Phone,

                    ROW_NUMBER()

                      OVER(ORDER BY LastName, FirstName, ContactID) AS SEQUENCE

             FROM   Person.Contact)

    SELECT Name = replace(FirstName + ' ' + isnull(MiddleName,'') + ' ' + LastName,

                          ' ',' '),

           [Email Address] = EmailAddress,

           Telephone = Phone

    FROM   cteContact

    WHERE  SEQUENCE BETWEEN @PageSize * @PageNumber + 1

    AND @PageSize * (@PageNumber + 1)

  END

 

GO

 

EXEC dbo.uspContactInfoByPage

  50 ,

  100

GO

 

/* Partial results

 

Name              Email Address                       Telephone

Kate  Becker      kate17@adventure-works.com          1 (11) 500 555-0141

Kate  Becker      kate17@adventure-works.com          1 (11) 500 555-0141

Kate  Becker      Akate17@adventure-works.com         NULL

Kate  Becker      Akate17@adventure-works.com         NULL

Kathryn  Becker   kathryn18@adventure-works.com       1 (11) 500 555-0110

*/

The World Leader in SQL Server 2008 Training
The future is just a CLICK away. Your future!
 
SQLUSA.com Home Page

Copyright 2005-2010, SMI Corp. All Rights Reserved.

SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.