An ASP. NET paging Stored Procedure

Source: Internet
Author: User
Alter procedure Pager
(
@ Pagesize int, -- number of lines per page
@ Pageindex int, -- current page number
@ Type int
)
As
Declare @ total int -- total number of rows
Declare @ totalpage int -- total number of pages
Select @ Total = count (*) from topic table where category = @ type -- total number of topics in the category of knowledge
 
Set @ totalpage = (@ total + @ pagesize-1)/@ pagesize; -- total number of pages = (total number of topics + number of lines per page-1)/number of lines per page
If (@ pageindex <= @ totalpage) -- if the current page is smaller than the total number of pages
Begin
Declare @ pagetable table (ID int identity (), pid int) -- create a temporary table
Insert into @ pagetable (PID)
Select ID from topic table where category = @ type order by posting time desc -- save the ID field of a topic table to a temporary table based on the posting time
Declare @ pagelower int -- upper limit of definition
Declare @ pageupper int -- defines the lower limit
Set @ pagelower = (@ pageindex-1) * @ pagesize + 1; -- for example, the number of lines per page is 10, the first page is from 1-10, and the second page is from 11-20
Set @ pageupper = @ pagelower + @ pagesize;

Select * From topic table where ID in
(Select PID from @ pagetable
Where ID >=@ pagelower and ID <@ pageupper) order by posting time DESC
End
Return @ totalpage;

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.