SQL Server 2012 (Denali) Ad-Hoc Query Paging

Order By has always been a query staple and now it has been complimented with some new arguments, OFFSET and FETCH.

The Ad-Hoc Paging feature allows you to write a query that returns a block data. For example the first execution of the query may return the first 5 records, the next execution returns the next 5 records and so on. You may also choose to write a query that starts returning data from perhaps the 10th or 20th record through to 100th or maybe the last record.

After the Order By clause an OFFSET argument can be used to specify the number of rows to be skipped before the query expression begins to return rows. The OFFSET value can be either an integer or an expression.

The FETCH argument follows the OFFSET argument specifying how my rows should be returned. The FETCH argument can also be an integer or an expression. The OFFSET argument does not require the FETCH argument however FETCH argument will cause an error without OFFSET.

OFFSET and FETCH returning results in pages of 5
Basic Ad Hoc query paging

OFFSET and FETCH can also use an expression
Basic AdHoc query paging

Download example here

Comments are closed.