SQL Server: General paging stored procedures implemented using system stored procedures

Source: Internet
Author: User
Code

Create   Proc Sp_pageview
@ SQL Ntext , -- SQL statement to be executed
@ Pagecurrent   Int = 1 , -- Page number to be displayed
@ Pagesize Int = 10 , -- Size of each page
@ Pagecount Int Output -- Total number of pages
As
Set Nocount On
Declare   @ P1   Int
-- Initialize paging cursor
Exec Sp_cursoropen
@ Cursor = @ P1 Output,
@ Stmt = @ SQL ,
@ Scrolopt = 1 ,
@ Ccopt = 1 ,
@ Rowcount = @ Pagecount Output

-- Total number of pages
If   Isnull ( @ Pagesize , 0 ) < 1  
Set   @ Pagesize = 10
Set   @ Pagecount = ( @ Pagecount + @ Pagesize - 1 ) / @ Pagesize
If   Isnull ( @ Pagecurrent , 0 ) < 1   Or   Isnull ( @ Pagecurrent , 0 ) > @ Pagecount
Set   @ Pagecurrent = 1
Else
Set   @ Pagecurrent = ( @ Pagecurrent - 1 ) * @ Pagesize + 1

--Display data on a specified page
ExecSp_cursorfetch@ P1,16,@ Pagecurrent,@ Pagesize

--Disable paging cursor
ExecSp_cursorclose@ P1

 

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.