SQL Universal Paging Code

Source: Internet
Author: User

Go----Universal Pagination code
CREATE PROCEDURE [dbo]. [Sp_datapager]
@pagesize int,--The size of each page
@pageindex int,--page number
Name of the @tablename varchar (Max),--table
@keycolumn varchar,---PRIMARY Key ID
@columns varchar,--to query the name of the Dequeue
@where varchar,---query criteria
@orderby varchar,---Sorting method
@recordcount int out--output parameter, not 0 returns the total number of records to query the table

As
declare @sql nvarchar (3000)
declare @rcsql nvarchar (1000)
Set @rcsql = ' Select @rc =count (*) from ' [email protected]
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + ' + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [email protected]+ ') '
if (@where! = ")
Begin
Set @rcsql = ' Select @rc =count (*) from ' [Email protected]+ ' where ' [email protected]
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [email protected]+ ' WHERE ' [email protected]+ ') and ' [email protected]
End
if (@orderby! = ")
Begin
if (@where! = ")
Begin
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [e-mail protected]+ ' where ' [email protected]+ ' ORDER BY ' [email protected]+ ') and ' +
@where + ' ORDER BY ' [email protected]
End
Else
Begin
Set @sql = ' Select top ' +convert (varchar (3), @pagesize) + @columns + ' from ' +
@tablename + ' where ' [email protected]+ ' Not in (select top ' +
CONVERT (varchar), (@pageindex-1) * @pagesize) + "[Email protected]+
' From ' [e-mail protected]+ ' ORDER by ' [e-mail protected]+ ') ' + ' ORDER BY ' [email protected]
End

End
declare @param nvarchar (100)
Set @param = ' @rc int output '
EXEC sp_executesql @sql
EXEC sp_executesql @rcsql, @param, @[email protected] Output

SQL Universal Paging Code

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.