42

How do I page results in SQL Server 2005?

I tried it in SQL Server 2000, but there was no reliable way to do this. I'm now wondering if SQL Server 2005 has any built in method?

What I mean by paging is, for example, if I list users by their username, I want to be able to only return the first 10 records, then the next 10 records and so on.

6 Answers 6

36

You can use the Row_Number() function. Its used as follows:

SELECT Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName
FROM Users

From which it will yield a result set with a RowID field which you can use to page between.

SELECT * 
FROM 
    ( SELECT Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName
      FROM Users 
    ) As RowResults
WHERE RowID Between 5 AND 10

etc

4
  • Excellent, simple example Pat - exactly what i was after :)
    – Town
    Sep 18, 2009 at 9:59
  • This answer doesn't work for me, though it did get me closer. It complains that it doesn't know what RowID is. See my answer below for additional info, if you have the same problem.
    – Beska
    Sep 25, 2009 at 19:02
  • 2
    In the inner select, you can select TOP X rows (X=max row wanted, in this case - 10). It will improve the speed of the query.
    – Faruz
    Jan 5, 2010 at 8:18
  • As the commenter above me mentioned, improve performance of this code by adding TOP to the inner table: SELECT TOP 10 Row_Number() OVER... You'll only grab the rows that will be needed, instead of the entire table.
    – Doug S
    Jul 25, 2012 at 22:35
13

If you're trying to get it in one statement (the total plus the paging). You might need to explore SQL Server support for the partition by clause (windowing functions in ANSI SQL terms). In Oracle the syntax is just like the example above using row_number(), but I have also added a partition by clause to get the total number of rows included with each row returned in the paging (total rows is 1,262):

SELECT rn, total_rows, x.OWNER, x.object_name, x.object_type
FROM (SELECT COUNT (*) OVER (PARTITION BY owner) AS TOTAL_ROWS,
         ROW_NUMBER () OVER (ORDER BY 1) AS rn, uo.*
         FROM all_objects uo
         WHERE owner = 'CSEIS') x
WHERE rn BETWEEN 6 AND 10

Note that I have where owner = 'CSEIS' and my partition by is on owner. So the results are:

RN  TOTAL_ROWS  OWNER   OBJECT_NAME            OBJECT_TYPE
6   1262    CSEIS   CG$BDS_MODIFICATION_TYPES   TRIGGER
7   1262    CSEIS   CG$AUS_MODIFICATION_TYPES   TRIGGER
8   1262    CSEIS   CG$BDR_MODIFICATION_TYPES   TRIGGER
9   1262    CSEIS   CG$ADS_MODIFICATION_TYPES   TRIGGER
10  1262    CSEIS   CG$BIS_LANGUAGES            TRIGGER
4
  • 1
    +1 I was wondering how to get the total rows and the page without using a temp table. Thanks!!
    – dotjoe
    Dec 17, 2008 at 15:04
  • 2
    +1 nice - got it to work on sqlserver using COUNT(*) OVER (PARTITION BY NULL)
    – Hafthor
    Mar 20, 2009 at 19:27
  • 1
    +1 ... nice. Now to play with it :-) Are you aware of any performance hit using COUNT(*) OVER (...) ?
    – Callie J
    Nov 30, 2010 at 17:31
  • In general, anything you can do in SQL directly will be the best option. In other words, anything you can do in a 'single' SQL query will likely outperform a more programmatic approach whether it is Java/ pl/SQL C#. Read Mr. Kyte as often as possible: tkyte.blogspot.com/2006/10/slow-by-slow.html
    – Brian
    Dec 1, 2010 at 1:33
5

The accepted answer for this doesn't actually work for me...I had to jump through one more hoop to get it to work.

When I tried the answer

SELECT Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName
FROM Users
WHERE RowID Between 0 AND 9

it failed, complaining that it didn't know what RowID was.

I had to wrap it in an inner select like this:

SELECT * 
FROM
    (SELECT
    Row_Number() OVER(ORDER BY UserName) As RowID, UserFirstName, UserLastName
    FROM Users
    ) innerSelect
WHERE RowID Between 0 AND 9

and then it worked.

2

When I need to do paging, I typically use a temporary table as well. You can use an output parameter to return the total number of records. The case statements in the select allow you to sort the data on specific columns without needing to resort to dynamic SQL.

--Declaration--

--Variables
@StartIndex INT,
@PageSize INT,
@SortColumn VARCHAR(50),
@SortDirection CHAR(3),
@Results INT OUTPUT

--Statements--
SELECT @Results = COUNT(ID) FROM Customers
WHERE FirstName LIKE '%a%'

SET @StartIndex = @StartIndex - 1 --Either do this here or in code, but be consistent
CREATE TABLE #Page(ROW INT IDENTITY(1,1) NOT NULL, id INT, sorting_1 SQL_VARIANT, sorting_2 SQL_VARIANT)
INSERT INTO #Page(ID, sorting_1, sorting_2)
SELECT TOP (@StartIndex + @PageSize)
    ID,
    CASE
        WHEN @SortColumn='FirstName' AND @SortDirection='ASC' THEN CAST(FirstName AS SQL_VARIANT)
        WHEN @SortColumn='LastName' AND @SortDirection='ASC' THEN CAST(LastName AS SQL_VARIANT)
        ELSE NULL
    END AS sort_1,
    CASE
        WHEN @SortColumn='FirstName' AND @SortDirection='DES' THEN CAST(FirstName AS SQL_VARIANT)
        WHEN @SortColumn='LastName' AND @SortDirection='DES' THEN CAST(LastName AS SQL_VARIANT)
        ELSE NULL
    END AS sort_2
FROM (
    SELECT
        CustomerId AS ID,
        FirstName,
        LastName
    FROM Customers
    WHERE
        FirstName LIKE '%a%'
) C
ORDER BY sort_1 ASC, sort_2 DESC, ID ASC;

SELECT
    ID,
    Customers.FirstName,
    Customers.LastName
FROM #Page
INNER JOIN Customers ON
    ID = Customers.CustomerId
WHERE ROW > @StartIndex AND ROW <= (@StartIndex + @PageSize)
ORDER BY ROW ASC

DROP TABLE #Page
1
  • 3
    This is what you would do in Sql Server 2000, but 2005 version has a better solution using ROW_NUMBER function.
    – niaher
    Jun 29, 2009 at 1:15
0

I believe you'd need to perform a separate query to accomplish that unfortionately.

I was able to accomplish this at my previous position using some help from this page: Paging in DotNet 2.0

They also have it pulling a row count seperately.

0

Here's what I do for paging: All of my big queries that need to be paged are coded as inserts into a temp table. The temp table has an identity field that will act in a similar manner to the row_number() mentioned above. I store the number of rows in the temp table in an output parameter so the calling code knows how many total records there are. The calling code also specifies which page it wants, and how many rows per page, which are selected out from the temp table.

The cool thing about doing it this way is that I also have an "Export" link that allows you to get all rows from the report returned as CSV above every grid in my application. This link uses the same stored procedure: you just return the contents of the temp table instead of doing the paging logic. This placates users who hate paging, and want to see everything, and want to sort it in a million different ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.