Database paging Stored Procedure Code

Source: Internet
Author: User
Database paging Stored Procedure Code

Database paging Stored Procedure Code

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.