Old paging Stored Procedure

Source: Internet
Author: User

In ASP. the DataGrid in net controls its built-in paging function, which reads all the data from the database before paging. If the data volume is large, the default paging method is very inefficient. Let's see what pages we use in SQL 2000.

Create proc [DBO]. [page_table]
(
@ Pageno int, -- current page number. The minimum value is 1, indicating the first page.
@ Pagesize int, -- number of entries per page
@ Pagecount int out, -- get the total number of pages
)
As
Set nocount on -- the returned results do not contain information about the number of rows affected by the transact-SQL statement.

Declare @ start int, @ itemscount int
-- Retrieve the total number of pages
Select @ pagecount = ceiling (count (ID) + 0.0)/@ pagesize)
From table
Where condition
-- Paging
If (@ pageno <> 1) // whether it is the first page
Begin
Set @ itemscount = (@ pageNo-1) * @ pagesize + 1
Set rowcount @ itemscount
Select @ start = ID
From table
Where condition
Order by ID DESC

Set rowcount @ pagesize // Retrieve the number of entries displayed on each page
Fields required for SELECT query
From table
Where Id >= @ Start and other conditions // ID >=@ itemscount
Order by ID DESC
End
Else // query the first page
Begin
Set rowcount @ pagesize // obtain the number of entries displayed on each page
Fields required for SELECT query
From table
Where condition
Order by ID DESC
End
Set rowcount 0

In this way, only the "number of entries per page" is retrieved at a time, which greatly improves the efficiency.

 

 

 

 

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.