Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
--drop procedure P_page
CREATE PROCEDURE P_page
@Tables varchar (1000)--table name such as TestTable
@PrimaryKey varchar,--table's primary key, must be unique
@Sort varchar = NULL,--sort fields such as f_name ASC or F_name desc (Note that you can only have one sort field)
@CurrentPage int = 1,--Current page
@PageSize int =,---per page size
List of fields displayed @Fields varchar (1000) = ' * ',--
@Filter varchar (1000) = NULL,--conditional statement, without where, such as F_id>3
@Group varchar (1000) = NULL,--grouping field
@TotalPage int output--Total number of pages returned
With Encryption---encrypted storage
SET NOCOUNT on
Declare @intResult Int
DECLARE @sql nvarchar (4000)
If @Filter is null or @Filter = '
Set @Sql = ' Select @intResult = count (' + @PrimaryKey + ') from ' + @Tables
Set @Sql = ' Select @intResult = count (' + @PrimaryKey + ') from ' + @Tables + ' where + ' + @Filter
EXEC sp_executesql @sql, N ' @intResult int OUTPUT ', @intResult output--total number of records calculated
Select @TotalPage =ceiling ((@intResult +0.0)/@PageSize)--Calculate total pages
IF @Sort is NULL or @Sort = '
SET @Sort = @PrimaryKey
IF CHARINDEX (' DESC ', @Sort) >0
SET @strSortColumn = REPLACE (@Sort, ' DESC ', ')
SET @operator = ' <= '
IF CHARINDEX (' ASC ', @Sort) > 0
SET @strSortColumn = REPLACE (@Sort, ' ASC ', ')
SET @operator = ' >= '
SET @strSortColumn = @SORT
SET @operator = ' >= '
IF CHARINDEX ('. ', @strSortColumn) > 0
SET @SortTable = SUBSTRING (@strSortColumn, 0, CHARINDEX ('. ', @strSortColumn))
SET @SortName = SUBSTRING (@strSortColumn, CHARINDEX ('. ', @strSortColumn) + 1, LEN (@strSortColumn))
SET @SortTable = @Tables
SET @SortName = @strSortColumn
Select @type =t.name, @prec =c.prec
From sysobjects o
JOIN syscolumns C on o.id=c.id
JOIN systypes T on C.xusertype=t.xusertype
Where o.name = @SortTable and c.name = @SortName
IF CHARINDEX (' char ', @type) > 0
SET @type = @type + ' (' + CAST (@prec as varchar) + ') '
If @ @Error <> 0
Return @intResult---Total number of records returned
declare @TotalPage int, @RecordCount int
Set @TotalPage =1
Execute @RecordCount =dbo.p_page ' Jxcliushui ', ' id ', ' date desc ', 500, 15, ' * ', ' date >= ' 2007-10-01 ', ', ', @TotalPage Output
Return Result: 1308
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.