Click here to Skip to main content
15,894,017 members
Articles / Web Development / IIS

Using ROW_NUMBER() to paginate your data with SQL Server 2005 and ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
20 Nov 20054 min read 243K   3.9K   86  
With the release of SQL Server 2005, Microsoft introduces the long overdue ROW_NUMBER() function. In this article, we will walk through a C# implementation of pagination using the ROW_NUMBER() method.
��/****** Object:  StoredProcedure [dbo].[sp_getzipcodes]    Script Date: 11/20/2005 12:47:31 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- =============================================

-- Author:		JWA

-- Create date: 11.19.05

-- Description:	using rownumber

-- =============================================

CREATE PROCEDURE [dbo].[sp_getzipcodes] 

	-- Add the parameters for the stored procedure here

	@start int = 0

	

AS

BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	Set NOCOUNT ON



	SELECT TOP 20 * FROM 

	(

	SELECT zip,city,state,latitude,longitude,timezone,dst,

		   ROW_NUMBER() OVER (ORDER BY zip) AS num

		   FROM dbo.zipcode

	) AS a

	WHERE num > @start



END

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
For more articles please visit my website:
http://www.aprogrammersjournal.com

A Programmers Journal
RSS

Comments and Discussions