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