A Practical paging stored process for large data volumes

Source: Internet
Author: User

Create proc sp_PublicTurnPageWebSite (
@ TBName nvarchar (100) = '', -- table name, such as pinyin
@ PageSize int = 10, -- number of records per page. The default value is 10.
@ CurPage int = 1, -- indicates the current page 1
@ KeyField nvarchar (100) = 'id', -- key field name. The default value is ID. This field must be an index in a table or a field without duplicates or blank fields.
@ KeyAscDesc nvarchar (4) = 'asc ', -- ascending or descending of keywords. The default value is ascending ASC, and descending is DESC.
@ Fields nvarchar (500) = '*', -- specifies the selected column name. All columns are selected by default.
@ Condition nvarchar (200) = '', -- where Condition, empty by default
@ Order nvarchar (200) = ''-- sorting condition. The default value is null.
) With encryption
If @ TBName =''
Begin
Raiserror ('Please specify the table name! ', 11,1)
Return
End
If @ PageSize <= 0 or @ CurPage <0
Begin
Raiserror ('the number of current pages and records on each page must be greater than zero! ', 11,1)
Return
End
If @ KeyAscDesc = 'desc'
Set @ KeyAscDesc = '<'
Else
Set @ KeyAscDesc = '>'
If @ Condition <>''
Set @ Condition = 'where' + @ Condition
Declare @ SQL nvarchar (2000)

Set @ SQL =''
If @ CurPage = 1
Set @ SQL = @ SQL + 'select top' + cast (@ PageSize as nvarchar (20 )) + ''+ @ Fields + 'from' + @ TBName + @ Condition +'' + @ Order
Else
Begin
Declare @ iTopNum int
Set @ iTopNum = @ PageSize * (@ CurPage-1)
Set @ SQL = @ SQL + 'Clare @ sLastValue nvarchar (100) '+ char (13)
Set @ SQL = @ SQL + 'select top' + cast (@ iTopNum as nvarchar (20 )) + '@ sLastValue =' + @ KeyField + 'from' + @ TBName + @ Condition + ''+ @ Order + char (13)

Declare @ Condition2 nvarchar (200)
If @ Condition =''
Set @ Condition2 = 'where' + @ KeyField + @ KeyAscDesc + '@ sLastValue'
Else
Set @ Condition2 = 'and' + @ KeyField + @ KeyAscDesc + '@ slastvalue'
Set @ SQL = @ SQL + 'select top' + cast (@ PageSize as nvarchar (20 )) + ''+ @ Fields + 'from' + @ TBName + @ Condition + @ Condition2 + @ Order
End
EXECUTE sp_executesql @ SQL

Related Article

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.