Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Demo: Simplified Result Set Paging with SQL Server 2012

Download

Right click “Save as…”

This demo shows how to replace code that was based on CTEs and ROW_NUMBER() to provide query pagination in earlier versions of SQL Server, to use the new OFFSET and FETCH clauses provided by SQL Server 2012.

Dr Greg Low

SQL Server MVP and Microsoft RD

Tags:

Follow the Discussion

  • Roger Dohertyrdoherty Roger Doherty

    Check out all the related decks, demos and labs at http://bit.ly/sql2012cookbook.

  • MatthewMatthew

    Quite often to do pagination effectively, you also need to have the total number of rows that the query would return without implementing the pagination. The way to do this currently with MSSQL is to basically run the same query twice, but with one that doesn't use the WHERE clause to alter your result count.

    MySQL lets you do this much more succinctly, for example.

    SELECT SQL_CALC_FOUND_ROWS Column1, Column2, ColumnN, ...
    FROM myTable
    JOIN my2ndTable
    JOIN ...
    WHERE <complex where clause>
    ORDER BY <complex order by>
    LIMIT (@numItemsPerPage)
    OFFSET (@numItemsPerPage * (@page - 1))

    -- 10 results (if @numItemsPerPage were 10)

    SELECT FOUND_ROWS() as 'Total Rows'

    -- 'Total Rows' = 380

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.