Research on SQL Server paging Stored Procedure

Source: Internet
Author: User
Www. webdiyer. comControlsAspNetPagerSpGenerator generate SQLSEVER2005 and later versions of SQLServer paging storage process: alias (@ projectIDnvarchar (50), @ pagesizeint, @ pageindexint, @ docountbit) asif (@ docount1)

Http://www.webdiyer.com/Controls/AspNetPager/SpGenerator generation SQL Server paging Stored procedure SQL SEVER 2005 and later versions: create procedure GetCasesByProjectID (@ projectID nvarchar (50), @ pagesize int, @ pageindex int, @ docount bit) as if (@ docount = 1) sel

Http://www.webdiyer.com/Controls/AspNetPager/SpGenerator generation SQL Server PagingStorageProcess

SQL SEVER 2005 or later versions:

Create procedure GetCasesByProjectID
(@ ProjectID nvarchar (50 ),
@ Pagesize int,
@ Pageindex int,
@ Docount bit)
As

If (@ docount = 1)
Select count (*) from GenAccidentCase where gensitebasicid = @ projectid
Else
Begin
With temptbl (
SELECT ROW_NUMBER () OVER (order by AccidentDate desc) AS Row, * from GenAccidentCase O where gensitebasicid = @ projectid)
SELECT * FROM temptbl where Row between (@ pageindex-1) * @ pagesize + 1 and (@ pageindex-1) * @ pagesize + @ pagesize
End

SQL SEVER 2000:

Create procedure GetCasesByProjectID
(@ ProjectID nvarchar (50 ),
@ Pagesize int,
@ Pageindex int,
@ Docount bit)
As
Set nocount on
If (@ docount = 1)
Select count (*) from GenAccidentCase where gensitebasicid = @ projectid
Else
Begin
Declare @ indextable table (id int identity (1, 1), nid int)
Declare @ PageLowerBound int
Declare @ PageUpperBound int
Set @ PageLowerBound = (@ pageindex-1) * @ pagesize
Set @ PageUpperBound = @ PageLowerBound + @ pagesize
Set rowcount @ PageUpperBound
Insert into @ indextable (nid) select genAccidentCaseID from GenAccidentCase where gensitebasicid = @ projectid order by AccidentDate desc
Select * from GenAccidentCase O, @ indextable t where O. genAccidentCaseID = t. nid
And t. id between @ PageLowerBound + 1 and @ PageUpperBound order by t. id
End
Set nocount off

Enterprise reference:

CREATE procedure pagination1
(@ Pagesize int, -- page size, for example, per pageStorage20 records
@ Pageindex int -- current page number)
As set nocount on
Begin
Declare @ indextable table (id int identity (), nid int) -- Define table Variables
Declare @ PageLowerBound int -- defines the bottom code of this page.
Declare @ PageUpperBound int -- defines the top code of this page.
Set @ PageLowerBound = (@ pageindex-1) * @ pagesize
Set @ PageUpperBound = @ PageLowerBound + @ pagesize
Set rowcount @ PageUpperBound
Insert into @ indextable (nid) select gid from TGongwen where fariqi> dateadd (day,-365, getdate () order by fariqi desc
Select O. gid, O. mid, O. title, O. fadanwei, O. fariqi from TGongwen O, @ indextable t where O. gid = t. nid
And t. id> @ PageLowerBound and t. id <= @ PageUpperBound order by t. id
End
Set nocount off

Comments in the article:

AboveStorageProcessUsing the latest SQL SERVER technology-Table variables. This should be saidStorageProcessIt is also a very good PagingStorageProcess. Of course, in thisProcessYou can also write the TABLE variables in the temporary TABLE: create table # Temp. But it is obvious that in SQL SERVER, using temporary tables does not use Quick table variables. So I just started to use thisStorageProcessAnd the speed is better than that of the original ADO. But later, I found a better method than this method.

In a sense, the efficiency is not too high.

2. not in method:

Retrieve the n to m records from the publish table:

Select top m-n + 1 * FROM publish WHERE (id not in (select top n-1 id FROM publish ))

Keywords with id publish table

Comments in the article:

When I saw this article at the time, I was truly inspired by the spirit and thought that the idea was very good. Later, I was working on an office automation system (ASP. NET + C # + SQL server), suddenly remembered this article, I think if you modify this statement, this may be a very good PagingStorageProcessSo I searched for this article on the Internet. I did not expect this article to be found, but I found a page based on this statement.StorageProcess, ThisStorageProcessIt is also a popular PagingStorageProcess.

If not in is used and not in is used, indexes cannot be used. Therefore, the efficiency is a little lower.

3. max method:

Select top page size * from table1 where id>
(Select max (id) from
(Select top (page number-1) * page size) id from table1 order by id) as T)
Order by id

Comments in the article:

We know that we can use max (field) or min (field) to extract the maximum or minimum values of almost any field, so if this field is not repeated, then, we can use the max or min of these non-repeated fields as the watershed to make them a reference object for separating each page in the paging algorithm. Here, we can use the operator ">" or "<" to accomplish this mission, so that the query statement conforms to the SARG format. For example:

Select top 10 * from table1 where id> 200

This is a little more efficient. But I don't know how max works, and how it works.

TIPS:

1. An efficient paging algorithm-Positioning method.

Declare @ pageSize int -- returns the number of records on a page
Declare @ CurPage int -- page number (page number) 0: First page;-1 last page.
Declare @ Count int
Declare @ id int
Set @ pageSize = 10
Set @ CurPage = 1
-- Positioning
If @ CurPage =-1
Begin
-- Last page
Set rowcount @ pageSize
Select @ id = newsID from newsTemp order by newsID
End
If @ CurPage> 0
Begin
Set @ Count = @ pageSize * (@ CurPage-1) + 1
Set rowcount @ Count
Select @ id = newsID from newsTemp order by newsID desc
End

-- Return record

set rowcount @pageSize
select * from newsTemp where newsID <=@id order by newsID descset rowcount 0

Idea: This is the continuation of the above algorithm. That is to say, avoid using the not in and max methods. That is, the idea: Select top 10 * from table1 where id> 200, locate -- that is, locate the critical point of paging. After finding it, the rest is easy to do.

Disadvantages: The values of Single-field sorting and sorting fields cannot be repeated (not necessarily repeated, but a few can be repeated ).

2. General method-the Reverse Method

Sometimes the disadvantage of the "Positioning Method" is unacceptable, but it does not matter. You can use this method.

select * from table where id in
(
select top 10 ID from
(
select top 20 ID,addedDate from table
order by addedDate desc
) as aa order by addedDate
)
order by addedDate desc

ID is the primary key and addedDate is the sorting field.

Disadvantage: a primary key is required.

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.