Optimal ms SQL Server paging SQL statements

Source: Internet
Author: User
Feature: only one page of data is returned for a single query. Instead of getting all the data.
Note:
Pagesize: number of records displayed per page
Cureentpage: Current page number

Select * from (select top pagesize * cureentpage * From user_table order by id asc) as asystable order by id desc) as bsystable order by ID ASC

Example:
Assume that the database table is as follows:
User_table:
ID: primary key, auto-Increment
Username: character
Password: character

Assume there are 80 records, and 10 records are displayed on each page, with IDs ranging from 1 to 80.
Now the data on the third page is sorted in ascending order by ID: The obtained Record ID should be 21 to 30.

The statement should be:
Select * from (select top 10 * from (select top 30 * From user_table order by id asc) as asystable order by id desc) as bsystable order by ID ASC

The principle is as follows:
(1) first, 30 records (3*10) are retrieved in ascending order of IDS, that is: records with IDs between 1 and 30 (select top 30 * From user_table order by id asc)
(2) sort the 30 records in descending order by ID.
(3) then extract the first 10 records from the 30 records: The obtained records are: IDs are between 30-21. This is the data we need, but it is arranged in descending order and does not meet the requirements.
(4) Finally, we can obtain the data we need in the re-sorting process. The ID is between and 30.

Hope to help you. // Try the above. The following example shows how to use. jsp:
**********************
<% @ Page Language = "Java" Import = "Java. util. *, java. SQL. *" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<JSP: usebean id = "cn" Scope = "page" class = "myconnection. Conn"/> <! -- Reference the bean for database operations and complete it by yourself. I will not go into details here -->
<%
Int curpage = 1; // current page
Int page_record = 20; // number of records displayed on each page
// Use the following method (SQL query is completed, and the speed is fast)
Curpage = integer. parseint (request. getparameter ("page"); // obtain the passed value, the page to be displayed
Resultset rs = cn. rsexecutequery ("select top" + page_record + "* From tablename where id not in (select top" + (curpage * page_record) + "id from tablename order by id desc) order by id desc ");
// This query statement returns the 20 records on the 1000 page to be displayed. The general idea is that subqueries exclude all records before the records to be displayed, the parent query sorts the remaining records in descending order.
While (Rs. Next ){
Out. println (Rs. getint ("ID"). tostring ());
}
Rs. Close ();
%>

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.