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