SQL Server 2012 implements paging new syntax

Source: Internet
Author: User
Tags sql server books

Recently have been looking at SQL Server books, but look at the basic query flow, query in the work of the most, so it is important to correctly query the desired data.

See in the book in SQL Server 2012 a new implementation of paging Query method, feel more convenient than the original, so record, if can help everyone, is very happy ~

Before paging, I basically use the row_number () function, and in the where statement with between and to implement, each time must write subquery, feel very troublesome, example:

SELECT * from (    SELECTID, row_number () Over(ORDER  by [Datetime] DESC) asNum fromdbo.tbluser) asTWHERET.numbetween  One  and  -

In SQL Server 2012, you do not need to write subqueries, directly in the order BY statement can be implemented pagination! Cases:

 select   ID, row_number ()  over  (order  by  [ datetime  ]  desc ) as   num  from   Dbo.tbluser  order  by   Numoffset  10   ROWS  fetch  next  10  ROWS only 

Note that the syntax is to introduce two new parameter OFFSET and FETCH in the ORDER BY clause, so they must be used only in the order BY statement and must be used in conjunction to make paging work.

OFFSET specifies the number of rows to skip before returning the query results, and the number of rows returned after the fetch specifies offset.

SQL Server 2012 implements paging new syntax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.