Paging stored procedures (using stored procedures to implement database paging code)

Source: Internet
Author: User

Copy codeThe Code is as follows:
--*************************************** ****************
-- * Paging stored procedure *
-- * Sahara Forest *
-- * 2010-6-28 *
--*************************************** ****************

If exists (select * from sysobjects where type = 'p' and name = n' P _ Paging ')
Drop procedure P_Paging
Go

Create procedure P_Paging
@ SqlStr nvarchar (4000), -- query string
@ CurrentPage int, -- page N
@ PageSize int -- number of lines per page
As
Set nocount on
Declare @ P1 int, -- P1 is the cursor id
@ Rowcount int
Exec sp_cursoropen @ P1 output, @ SqlStr, @ scrolopt = 1, @ ccopt = 1, @ rowcount = @ rowcount output
Select ceiling (1.0 * @ rowcount/@ PageSize) as total number of pages --, @ rowcount as total number of rows, @ CurrentPage as current page
Set @ CurrentPage = (@ CurrentPage-1) * @ PageSize + 1
Exec sp_cursorfetch @ P1, 16, @ CurrentPage, @ PageSize
Exec sp_cursorclose @ P1
Set nocount off
Go


---- Create a test table
-- If exists (select * from sysobjects where type = 'U' and name = n' Test _ Students ')
-- Drop table Test_Students
-- Go
-- Create table Test_Students (
-- Id int IDENTITY (1, 1) not null,
-- Name nvarchar (100) not null
--)
--
---- Create Test Data
-- Declare @ I int
-- Set @ I = 100000
-- While @ I> 0
-- Begin
-- Insert into Test_Students values ('name ')
-- Set @ I = @ I-1
-- End
--
---- Execute the Stored Procedure
-- Exec P_Paging 'select * from Test_Students order by id', 100,100 -- run
--
---- Delete test table
-- If exists (select * from sysobjects where type = 'U' and name = n' Test _ Students ')
-- Drop table Test_Students
-- Go

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.