CopyCode The Code is as follows :/*
Database paging stored procedures, supporting reverse and ascending
Parameter description:
@ Tablename: name of the search table
@ Tablefield: Specifies the table field, which is the table's primary key,
@ Where: The name of the search table. to display all records, set it to "1 = 1"
@ Orderby: sorts search results, for example, order by ID DESC
@ Fieldlist: List of fields, such as userid and username
@ Curpage: Current page number
@ Page_record: number of records per page
@ Sort: Sorting identifier (for reverse sorting, the parameter value is desc, Which is ascending, and the parameter value is ASC, which corresponds to the orderby parameter)
Result: The page_record records on the page curpage that meet the where condition in the tablename table are returned. The results are ordered by orderby.
*/
Create procedure proc_commonpaging
@ Tablename varchar (100 ),
@ Tablefield varchar (20 ),
@ Where varchar (5000 ),
@ Orderby varchar (500 ),
@ Fieldlist varchar (1000 ),
@ Curpage int,
@ Page_record int,
@ Sort varchar (8)
As
Begin
Declare @ cmd varchar (8000)
Declare @ uprecord int
Declare @ op varchar (2) -- operator
Declare @ max_min varchar (4) -- Maximum/minimum calculation
Set @ op = '<'
Set @ max_min = 'Min'
If @ sort = 'asc'
Begin
Set @ op = '>'
Set @ max_min = 'Max'
End
Set @ uprecord = @ curpage * @ page_record
If @ curpage = 0
Set @ cmd = 'select top '+ Cast (@ page_record as nvarchar) + ''+ @ fieldlist + 'from' + @ tablename + 'where' + @ where +'' + @ orderby
Else
Set @ cmd = 'select top '+ Cast (@ page_record as nvarchar) + ''+ @ fieldlist + 'from' + @ tablename + 'where' + @ where + 'and' + @ tablefield +'
'+ @ OP +' (select '+ @ max_min +' ('+ @ tablefield +') from (select top '+ Cast (@ uprecord as nvarchar) + ''+ @ tablefield + 'from' + @ tablename + 'where
'+ @ Where + ''+ @ orderby +') as tmptbl) and '+ @ where +'' + @ orderby
Set @ cmd = @ cmd + '; select count (*) from' + @ tablename + 'where' + @ where
Exec (@ cmd)
Print (@ cmd)
End
Go