Code of a paging Stored Procedure

Source: Internet
Author: User

Copy codeThe Code is as follows:
------------------------------------
-- Purpose: Paging stored procedures (efficient for tables with primary keys)
-- Note:
------------------------------------
Alter procedure [UP_GetRecordByPage]
@ TblName varchar (255), -- table name
@ FldName varchar (255), -- primary key field name
@ PageSize int = 10, -- page size
@ PageIndex int = 1, -- page number
@ IsReCount bit = 0, -- returns the total number of records. If the value is not 0, the system returns
@ OrderType bit = 0, -- set the sorting type. If the value is not 0, the sorting type is descending.
@ StrWhere varchar (1000) = ''-- Query condition (Note: Do not add where)
AS
Declare @ strSQL varchar (6000) -- subject sentence
Declare @ strTmp varchar (100) -- Temporary Variable
Declare @ strOrder varchar (400) -- sort type
If @ OrderType! = 0
Begin
Set @ strTmp = '<(select min'
Set @ strOrder = 'order by ['+ @ fldName +'] desc'
End
Else
Begin
Set @ strTmp = '> (select max'
Set @ strOrder = 'order by ['+ @ fldName +'] asc'
End
Set @ strSQL = 'select top '+ str (@ PageSize) +' * from ['
+ @ TblName + '] where [' + @ fldName + ']' + @ strTmp + '(['
+ @ FldName + ']) from (select top' + str (@ PageIndex-1) * @ PageSize) + '['
+ @ FldName + '] from [' + @ tblName + ']' + @ strOrder + ') as tblTmp )'
+ @ StrOrder
If @ strWhere! =''
Set @ strSQL = 'select top '+ str (@ PageSize) +' * from ['
+ @ TblName + '] where [' + @ fldName + ']' + @ strTmp + '(['
+ @ FldName + ']) from (select top' + str (@ PageIndex-1) * @ PageSize) + '['
+ @ FldName + '] from [' + @ tblName + '] where' + @ strWhere +''
+ @ StrOrder + ') as tblTmp) and' + @ strWhere + ''+ @ strOrder
If @ PageIndex = 1
Begin
Set @ strTmp =''
If @ strWhere! =''
Set @ strTmp = 'where' + @ strWhere
Set @ strSQL = 'select top '+ str (@ PageSize) +' * from ['
+ @ TblName + ']' + @ strTmp + ''+ @ strOrder
End
If @ IsReCount! = 0
Set @ strSQL = 'select count (*) as Total from ['+ @ tblName +'] '+ 'where' + @ strWhere
Exec (@ strSQL)

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.