MySQL stored procedure Efficient paging method

Source: Internet
Author: User

Create procedure [dbo]. [Prgs_nation_task_getlist]
@PageSize int = 100--Displays the number of record bars per page, defaults to 100
@PageIndex int = 1,--current fetch page number to display, default is 1, database returns one page of data based on Pagesize,pageindex calculation
@RetTotal int output,--Total number of records
@RetCount int output,--Returns the number of records
@RetPageIndex int output,--Print current page number
@ReturnDesc varchar (128) Output--Returns the description of the operation result
As
Begin
SET NOCOUNT ON
Set XACT_ABORT on
Set @RetTotal = 0
Set @RetCount = 0
Set @RetPageIndex = @PageIndex


-Multiple conditional values
declare @Err INT--Error
Declare @PageCount INT--Total pages
Declare @BeginRID INT--Start row Rid
DECLARE @MaxRow INT--Last line
Select @RetTotal = count (*)
from nationtask
Select @Err = @ @ERROR
If @Err <> 0
Begin
Set @ReturnDesc = ' Extract total number of national tasks failed! '
return-1
End
--if there is no data, returns an empty result set
If @RetTotal = 0
Begin
Set @ReturnDesc = ' current condition has no national task record! '
return 1
End
-count total pages
Set @PageCount = @RetTotal/@PageSize
If @RetTotal% @PageSize > 0 begin
Set @PageCount = @PageCount + 1
End
--more than the total number of pages, returns an empty result set
if @PageIndex > @PageCount
Begin
Set @ReturnDesc = ' current condition has no national task record! '
return 1
End
--Gets the Rid
Set @MaxRow = @PageSize * (@PageIndex-1) + 1
Set Rowcoun for the first row of records to be returned to the page T @MaxRow
Select @BeginRID = Rid
from nationtask
ORDER by rid Desc

--Returns the list of data
SET ROWCOUNT @PageSize
Select Rid
, TaskName
, Tasktitle
, imageID
, effectid
, StartTime the
from Nationtask
where rid <= @BeginRID the
ORDER by Rid desc
Set @RetCount = @ @rowcount
--end
Set @ReturnDesc = ' Extract national Task list successfully! '
return 1
End

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.