Paging query efficiency highest _MSSQL

Source: Internet
Author: User
Tags rowcount first row
Copy Code code as follows:

/*

Date: 2009-03-19
Function: Get the Game country task list data according to various conditions
*/
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




--Multi-condition value
DECLARE @Err INT--Error
DECLARE @PageCount INT--Total pages
DECLARE @BeginRID INT--Start the Rid
DECLARE @MaxRow INT--Last line

Select @RetTotal = COUNT (*)
From Nationtask

Select @Err = @ @ERROR

If @Err <> 0
Begin
Set @ReturnDesc = ' Extract the total number of national tasks failed! '
Return-1
End

--Returns an empty result set if no data is available
If @RetTotal = 0
Begin
Set @ReturnDesc = ' current condition has no national task record! '
Return 1
End

--Calculate Total pages
Set @PageCount = @RetTotal/@PageSize

If @RetTotal% @PageSize > 0
Begin
Set @PageCount = @PageCount + 1
End

--Returns an empty result set if the total number of pages is exceeded
If @PageIndex > @PageCount
Begin
Set @ReturnDesc = ' current condition has no national task record! '
Return 1
End

--Gets the Rid of the first row of records to return the page
Set @MaxRow = @PageSize * (@PageIndex-1) + 1

SET ROWCOUNT @MaxRow

Select @BeginRID = Rid
From Nationtask
ORDER BY Rid Desc


--return list of data
SET ROWCOUNT @PageSize

Select Rid
, TaskName
, Tasktitle
, imageID
, effectid
, StartTime
From Nationtask
where Rid <= @BeginRID
ORDER BY Rid Desc

Set @RetCount = @ @rowcount

--End
Set @ReturnDesc = ' Extract national Task List success! '
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.