/*
Function Name: sp_getrecordfrompage
Function: obtains data on a specified page.
Parameter description: @ tblname indicates the name of the table containing data.
@ Fldname key field name
@ Pagesize number of records per page
@ Pageindex: page number to be obtained
@ Ordertype: Sorting type. Values: 0 (ascending) and 1 (descending ).
@ Strwhere query condition (Note: Do not add the where value '')
*/
Create Procedure Sp_getrecordfrompage
@ Tblname Varchar ( 255 ), -- Table Name
@ Fldname Varchar ( 255 ), -- List of field names to be sorted. Multiple fields are separated by commas.
@ Pagesize Int = 10 , -- The default page size is 10.
@ Pageindex Int = 1 , -- The default page size is 1.
@ Ordertype Bit = 0 , -- Set the sorting type. If the value is not 0, sort it in descending order.
@ Strwhere Varchar (2000 ) = '' -- Query conditions (Note: Do not add where)
As
Declare @ Strsql Varchar ( 6000 ) -- Subject sentence
Declare @ Strtmp Varchar ( 1000 ) -- Temporary Variable
Declare @ Strorder Varchar ( 500 ) -- Sorting type
If @ Ordertype ! = 0
Begin
Set @ Strtmp = ' <(Select Min '
Set @ Strorder = ' Order [ ' + @ Fldname + ' ] DESC '
End
Else
Begin
Set @ Strtmp = ' > (Select Max '
Set @ Strorder = ' Order ' + @ Fldname + ' ASC '
End
Set @ Strsql = ' Select top ' + Str ( @ Pagesize ) + ' * From [ '
+ @ Tblname + ' ] Where [ ' + @ Fldname + ' ] ' + @ Strtmp + ' ([ '
+ @ Fldname + ' ]) From (select top ' + Str (( @ Pageindex - 1 ) * @ Pagesize ) + ' [ '
+ @ Fldname + ' ] From [ ' + @ Tblname + ' ] ' + @ Strorder + ' ) As tbltmp) '
+ @ Strorder
If @ Strwhere ! = ''
Set @ Strsql = ' Select top ' + Str ( @ Pagesize ) + ' * From [ '
+ @ Tblname + ' ] Where [ ' + @ Fldname + ' ] ' + @ Strtmp + ' ([ '
+ @ Fldname + ' ]) From (select top ' + Str (( @ Pageindex - 1 ) * @ Pagesize ) + ' [ '
+ @ Fldname + ' ] From [ ' + @ Tblname + ' ] Where ' + @ Strwhere + ' '
+ @ Strorder + ' ) As tbltmp) and ' + @ Strwhere + ' ' + @ Strorder
If @ Pageindex = 1
Begin
Set @ Strtmp = ''
If @ Strwhere ! = ''
Set @ Strtmp = ' Where ( ' + @ Strwhere + ' ) '
Set @ Strsql = ' Select top ' + Str ( @ Pagesize ) + ' * From [ '
+ @ Tblname + ' ] ' + @ Strtmp + ' ' + @ Strorder
End
Exec ( @ Strsql )
Go