Blog Post

Demo: Simplified Result Set Paging with SQL Server 2012

Download this episode

Download Video

Description

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

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      rdoherty

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

    • User profile image
      Matthew

      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

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.