SQL Server paging stored procedures (available for test and available for sorting pages of non-dimension One-value fields) are the most active, with poor performance

Source: Internet
Author: User

Use [test]
Go
/***** Object: storedprocedure [DBO]. [test1_sp_pageview] script Date: 10/09/2010 09:55:50 ******/
Set ansi_nulls on
Go
Set quoted_identifier on
Go
Create proc [DBO]. [test1_sp_pageview]
@ SQL ntext, -- the SQL statement to be executed
@ Pagecurrent Int = 1, -- the 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 the paging cursor
Exec sp_cursoropen
@ Cursor = @ P1 output,
@ Stmt = @ SQL,
@ Scrolopt = 1,
@ Ccopt = 1,
@ Rowcount = @ pagecount output

-- Calculate the 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
Exec sp_cursorfetch @ P1, 16, @ pagecurrent, @ pagesize

-- Disable paging cursor
Exec sp_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.