SQLUSA

Microsoft SQL Server 2005 Best Practices

How to paginate company contact information?

 

Execute the following script in Query Editor to create pagination for the Person.Contact table info by applying the ROW_NUMBER() function:


CREATE PROCEDURE procContactInfoByPage
@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.procContactInfoByPage 50,100


 

SQLUSA - The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page