Create procedure sp_page
@ Tb varchar (50), -- table name
@ Col varchar (50), -- pagination by this column
@ Coltype int, -- @ col column type, 0-number type, 1-character type, 2-Date and Time Type
@ Orderby bit, -- sort, 0-order, 1-inverted
@ Collist varchar (800), -- List of fields to be queried, * indicates all fields
@ Pagesize int, -- number of records per page
@ Page int, -- specify the page
@ Condition varchar (800), -- Query condition
@ Pages int OUTPUT -- total number of pages
AS
/*
Function Description: queries records that meet the specified conditions in a specified table by Page Based on the specified column. The page can be sorted in reverse order.
Query can specify the page size, specify any page to query, specify the list of output fields, and return the total number of pages
Author: pbsql
Version 1.10
Last modified: 2004-11-29
*/
DECLARE @ SQL nvarchar (4000), @ where1 varchar (800), @ where2 varchar (800)
IF @ condition is null or rtrim (@ condition) =''
BEGIN -- no query Conditions
SET @ where1 = 'where'
SET @ where2 =''
END
ELSE
BEGIN -- with query Conditions
SET @ where1 = 'where ('+ @ condition +') AND '-- this condition is added if conditions exist.
SET @ where2 = 'where ('+ @ condition +') '-- this condition is added if no conditions exist.
END
SET @ SQL = 'select @ pages = CEILING (COUNT (*) + 0.0)/'+ CAST (@ pagesize AS varchar) +
') FROM' + @ tb + @ where2
EXEC sp_executesql @ SQL, n' @ pages int output', @ pages OUTPUT -- calculate the total number of pages
IF @ orderby = 0
SET @ SQL = 'select TOP '+ CAST (@ pagesize AS varchar) + ''+ @ collist +
'From' + @ tb + @ where1 + @ col + '> (select max (' + @ col + ')' +
'From (select top '+ CAST (@ pagesize * (@ page-1) AS varchar) + ''+
@ Col + 'from' + @ tb + @ where2 + 'ORDER BY' + @ col + ') t) ORDER by' + @ col
ELSE
SET @ SQL = 'select TOP '+ CAST (@ pagesize AS varchar) + ''+ @ collist +
'From' + @ tb + @ where1 + @ col + 'min ('+ @ col +') '+
'From (select top '+ CAST (@ pagesize * (@ page-1) AS varchar) + ''+
@ Col + 'from' + @ tb + @ where2 + 'ORDER BY' + @ col + 'desc) t) ORDER by' +
@ Col + 'desc'
IF @ page = 1 -- first page
SET @ SQL = 'select TOP '+ CAST (@ pagesize AS varchar) + ''+ @ collist + 'from' + @ tb +
@ Where2 + 'ORDER BY' + @ col + CASE @ orderby WHEN 0 then' 'else' DESC 'end
EXEC (@ SQL)
GO
This storage process is efficient. We used 5 million pieces of data for testing (with indexes already created). Only 3 seconds of paging is returned. The effect is on the total number of pages to be calculated. you can comment it out if you do not need it.
-- Test example
Declare @ pages int
Select identity (int, 1, 1) id, getdate () dt, xx = cast (''as varchar (10) into # t
From sysobjects
Update # t set dt = dateadd (day, id-200, dt ),
Xx = 'xxxx' + right ('000000' + cast (id as varchar (10), 6)
Exec sp_page '# t', 'id',' * ', '', @ pages output -- Take the second page by id
Exec sp_page '# t', 'id',' * ', '', @ pages output -- retrieves the second page by id in reverse order
Exec sp_page '# t', 'XX',' * ', '', @ pages output -- take the third page in xx order
Exec sp_page '# t', 'XX',' * ', '', @ pages output -- retrieves the third page in reverse order of xx
Exec sp_page '# t', 'dt',' * ', '', @ pages output -- Take the second page in dt order
Exec sp_page '# t', 'dt',' * ', '', @ pages output -- retrieves the second page by dt in reverse order
Total number of select pages = @ pages
Drop table # t
-- From csdn pbsql (Fengyun) Post http://blog.csdn.net/pbsql/archive/2004/11/30/199657.aspx