solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page








Page through SQL Server results with the ROW_NUMBER() Function

By: | Read Comments | Print

Jeremy is the CTO @ Edgewood Solutions, co-founder of MSSQLTips.com and SQL Server MVP since 2009.



Related Tips: More

Problem
For many applications searching and providing a subset of a large result set is the core application functionality.  Often the result sets are large and it is a resource intensive process to gather and display the data.  This is because the same query is issued each time a new page is rendered, but a different portion of the result set is displayed.  Once this technique becomes too much of a performance burden, custom solutions are built to meet the need.  With all of the improvements in SQL Server 2005, does a simpler and less resource intensive approach exist to efficiently page through a large result set?

Solution
Yes - SQL Server 2005 now ships with the ROW_NUMBER function which is directly supported in T-SQL.  The ROW_NUMBER function provides the ability to issue a query and just return a subset of the result set.  This is achieved by using the partition clause and order by clause of the ROW_NUMBER function.   In the example below, the partition clause is RowNumber and the order by clause is the OrderDate.  The OVER clause is used to determine the partitioning and ordering of the intermediary result set before the ROW_NUMBER function is applied.  This is wrapped by the WITH clause which is being used as a common table expression (CTE) for a temporary named result set.  With this being said, in the last SELECT statement the CTE is queried and only rows 50 to 60 are returned.

USE AdventureWorks;
GO
WITH OrderedOrders AS
(
     SELECT SalesOrderID, OrderDate,
     ROW_NUMBER() OVER (order by OrderDate) AS 'RowNumber'
     FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber between 50 and 60;
GO

Source - SQL Server 2005 Books Online - ROW_NUMBER (Transact-SQL)

The example above is simple and straight forward.  This code can be greatly enhanced by passing in parameters and further customized to meet your specific paging needs, see the related articles in the 'Next Steps' section below.  Although an initial usage of this code would be for paging through records in a web based application, think about some of the application challenges you have faced and consider the ROW_NUMBER function as a simple means to address these issues.

Next Steps



Related Tips: More | Become a paid author


Last Update: 2/8/2007

Share: Share 






Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Try the free performance monitoring tool from Idera!

Write, edit, and explore SQL effortlessly with SQL Prompt.

What grade do you think your SQL Servers get? Find out with Edgewood's SQL Server Health Check consulting services.

Get SQL Server Tips Straight from Kevin Kline.

Join the over million SQL Server Professionals who get their issues resolved daily.

Free web casts for DBAs and Developers on Performance Tuning, Development, Administration and more....


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com