Microsoft SQL Server 2005 Best Practices

How to page through a SELECT result set?

 

Execute the following script in Query Editor to create a paging stored procedure for sales order detail:

Use AdventureWorks;

Create Proc dbo.procSalesGetPageOfData
(
@PageSize as int,
@PageNo int
)
As
Begin

With ctePaging
As
(
Select
Top(@PageSize * @PageNo) Row_number()
Over (Order by SalesOrderID, SalesOrderDetailID) as SeqNo,
OrderQty, UnitPrice, LineTotal, ModifiedDate
From Sales.SalesOrderDetail
)
Select * from ctePaging
where SeqNo > ((@PageNo - 1) * @PageSize)

End
go

-- exec dbo.procSalesGetPageOfData 10,351

 

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