Database paging Stored Procedure Code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.