SQL Server paging stored procedures

Source: Internet
Author: User

A little bit of the online circulation of the general change, even the results of the table query can be returned

Create PROC [dbo]. [PageView] (@sql nvarchar (max),---original query Statement @pageindex int,--page number @pagesize int,--Number of records per page @sort VARCHAR (255),--sort fields and rules without order By@getcount bit--whether to get the total number of records 1 for the total number of records, 0 to not get the total number of records, return recordset) asdeclare @strSql nvarchar (max) set NOCOUNT on;if (@GetCount = 1) Beginset @strSql = ' Select COUNT (*) as GetRowCount from (' [E-mail protected]+ ') t ' endelsebeginset @strSql = ' select * FROM (S Elect Row_number () over (ORDER by ' + @Sort + ') as RowNum, * from (' [email protected]+ ') as Dwhere) Twhere T.rownum Betwe EN ' + CAST (((@PageIndex-1) * @PageSize + 1) as varchar (+) + ' and ' + CAST ((@PageIndex * @PageSize) as varchar ()) endexec (@strSql) set NOCOUNT off;

  

Take a look.

EXEC book.dbo.PageView ' select * from book where id<58385557 ',, ' id desc ', 1  exec book.dbo.PageView ' select * FROM Book where id<58385557 ',,, ' id desc ', 0

  

Results

SQL Server paging stored procedures

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.