SQLUSA

Microsoft SQL Server 2005 Best Practices

Microsoft SQL Server 2000 Best Practices

How to page in large result set?

 

Execute the following script in Query Analyzer or Query Editor to demonstrate paging a large result set. The inner most query (derived table x) can be replaced.

USE Northwind;

 

DECLARE @LinesPerPage int,

            @PageNumber int,

            @StartRow int

SET @LinesPerPage = 40

SET @PageNumber =10

SET @StartRow = @PageNumber * @LinesPerPage

 

SELECT * FROM

      (SELECT TOP (@LinesPerPage) * FROM

            (SELECT TOP (@StartRow) *

            FROM [Order Details]

            WHERE UnitPrice > $10.0

            ORDER BY 1 ) x

      ORDER by 1 DESC) y

ORDER by 1

 

The Best SQL Server Training in the World
The future is just a click away! Your Future!
SQLUSA.com Home Page