Problem solve Get help with specific problems with your technologies, process and projects.

Paging results using T-SQL

Get a page of results from the database with T-SQL.

In the world of Internet development, there are many times when a web page needs to display a subset of a large result set, that is, "paged results." Microsoft's ADO and the .NET platform offer this as a feature of the datagrid class, however the problem with this is that the complete recordset (containing all the data) is sent to the web server each time a page is viewed. This has an impact on web server memory and the bandwidth between the web server and the database server (if they are on separate machines)

The following SQL allows a page of results to be extracted from the database, so that only the single page of results is sent to the web server, thus reducing bandwidth requirements and memory requirements on the web server.

I have seen other variants of paged results built predominantly in SQL, but they tend to use cursors, which are notoriously slow. This solution creates a temporary table "#list" of a filtered set of results from the primary table, which in this case selects all the results from "ORDERS" (from Northwind database) where the customer id = "LINOD". The ORDERS table can be sorted if required. The temporary table #list contains only two columns, a unique identity and the primary key from the orders table. Once the temporary table is created, it can be filtered such that is between the starting record and the ending record for the page, and can be joined back to the "orders" table if there are other columns needed in the results.

The SQL has been tested on SQL server 2000. It uses the standard SQL functions that are available in SQL server 7.0, so there is no reason why it wouldn't work on SQL 7.0.

declare @pagerecords int
set @pagerecords = 5
declare @page int
set @page = 3
declare @totalrecords int

set rowcount 0
create table #list (id int primary key identity(1,1),uid int)
insert into #list
select OrderID from orders --place the primary table here
--join other tables here that may contain information to be filtered by or sorted by.
 where CustomerID = 'LINOD'

--Grab the total records here, will be useful if we need to display the total
--number of records or pages.
set @totalrecords = @@rowcount

--Set the number of records to display in a page.
set rowcount @pagerecords
--Now do the actual select query.
select *,@totalrecords from #list l
 join orders o on o.OrderID = l.uid
--Join other tables here that may contain information that needs to be displayed.
where > ((@page-1) * @pagerecords)
--Other where clauses here if you like.

drop table #list

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, SQL Server, DB2, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

Dig Deeper on SQL Server and .NET development

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.