CREATE PROCEDURE prcPageResult -- 获得某一页的数据 --
@currPage INT = 1 , --当前页页码 (即Top currPage)
@showColumn VARCHAR(2000) = ‘*‘ , --需要得到的字段 (即 column1,column2,......)
@tabName VARCHAR(2000) , --需要查看的表名 (即 from table_name)
@strCondition VARCHAR(2000) = ‘‘ , --查询条件 (即 where condition......) 不用加where关键字
@ascColumn VARCHAR(100) = ‘‘ , --排序的字段名 (即 order by column asc/desc)
@bitOrderType BIT = 0 , --排序的类型 (0为升序,1为降序)
@pkColumn VARCHAR(50) = ‘‘ , --主键名称
@pageSize INT = 20 --分页大小
AS
BEGIN -- 存储过程开始 -- 该存储过程需要用到的几个变量 --
DECLARE @strTemp VARCHAR(1000)
DECLARE @strSql VARCHAR(4000) --该存储过程最后执行的语句
DECLARE @strOrderType VARCHAR(1000) --排序类型语句 (order by column asc或者order by column desc)
BEGIN
IF @bitOrderType = 1 -- bitOrderType=1即执行降序
BEGIN
SET @strOrderType = ‘ ORDER BY ‘ + @ascColumn + ‘ DESC‘
SET @strTemp = ‘<(SELECT min‘
END
ELSE
BEGIN
SET @strOrderType = ‘ ORDER BY ‘ + @ascColumn + ‘ ASC‘
SET @strTemp = ‘>(SELECT max‘
END
IF @currPage = 1 -- 如果是第一页
BEGIN
IF @strCondition != ‘‘
SET @strSql = ‘SELECT TOP ‘ + STR(@pageSize) + ‘ ‘
+ @showColumn + ‘ FROM ‘ + @tabName + ‘ WHERE ‘
+ @strCondition + @strOrderType
ELSE
SET @strSql = ‘SELECT TOP ‘ + STR(@pageSize) + ‘ ‘
+ @showColumn + ‘ FROM ‘ + @tabName
+ @strOrderType
END
ELSE -- 其他页
BEGIN
IF @strCondition != ‘‘
SET @strSql = ‘SELECT TOP ‘ + STR(@pageSize) + ‘ ‘
+ @showColumn + ‘ FROM ‘ + @tabName + ‘ WHERE ‘
+ @strCondition + ‘ AND ‘ + @pkColumn + @strTemp
+ ‘(‘ + @pkColumn + ‘)‘ + ‘ FROM (SELECT TOP ‘
+ STR(( @currPage - 1 ) * @pageSize) + ‘ ‘
+ @pkColumn + ‘ FROM ‘ + @tabName + @strOrderType
+ ‘) AS TabTemp)‘ + @strOrderType
ELSE
SET @strSql = ‘SELECT TOP ‘ + STR(@pageSize) + ‘ ‘
+ @showColumn + ‘ FROM ‘ + @tabName + ‘ WHERE ‘
+ @pkColumn + @strTemp + ‘(‘ + @pkColumn + ‘)‘
+ ‘ FROM (SELECT TOP ‘ + STR(( @currPage - 1 )
* @pageSize) + ‘ ‘
+ @pkColumn + ‘ FROM ‘ + @tabName + @strOrderType
+ ‘) AS TabTemp)‘ + @strOrderType
END
END
EXEC (@strSql)
END
EXEC prcPageResult 2,‘*‘,‘outpr_register‘,‘state=1‘,‘inputtime‘,1,‘registerid‘,3