www.9778.com:AspNetPager 通用性分页存储过程

一篇好用的通用分页存储过程,可以用在任何开发上哦,只要小小的改动就好了,有需要的朋友可以参考一下本款实例。
代码如下复制代码 /*通用分页存储过程*/USE HotelManagementSystemGOIF
EXISTS(SELECT * FROM sys.objects WHERE
NAME=’cndoup_GetPageOfRecords’)DROP PROCEDURE
cndoup_GetPageOfRecordsGO–创建存储过程CREATE PROCEDURE
cndoup_GetPageOfRecords@pageSize int = 20, –分页大小@currentPage int ,
–第几页@columns varchar(1000) = ‘*’, –需要得到的字段 @tableName
varchar(100), –需要查询的表 @condition varchar(1000) = ”, –查询条件,
不用加where关键字@ascColumn varchar(100) = ”, –排序的字段名 (即 order
by column asc/desc)@bitOrderType bit = 0, –排序的类型
(0为升序,1为降序)@pkColumn varchar(50) = ” –主键名称ASBEGIN
–存储过程开始DECLARE @strTemp varchar(300)DECLARE @strSql varchar(5000)
–该存储过程最后执行的语句DECLARE @strOrderType varchar(1000)
–排序类型语句 (order by column asc或者order by column desc)BEGINIF
@bitOrderType = 1 –降序BEGINSET @strOrderType = ‘ ORDER BY
‘+@ascColumn+’ DESC’SET @strTemp = ‘(SELECT min’ENDELSE–升序BEGINSET
@strOrderType = ‘ ORDER BY ‘+@ascColumn+’ ASC’SET @strTemp = ‘(SELECT
max’ENDIF @currentPage = 1–第一页BEGINIF @condition != ”SET @strSql =
‘SELECT TOP ‘+STR(@pageSize)+’ ‘+@columns+’ FROM ‘+@tableName+’ WHERE
‘+@condition+@strOrderTypeELSESET @strSql = ‘SELECT TOP
‘+STR(@pageSize)+’ ‘+@columns+’ FROM ‘+@tableName+@strOrderTypeENDELSE–
其他页BEGINIF @condition !=”SET @strSql = ‘SELECT TOP
‘+STR(@pageSize)+’ ‘+@columns+’ FROM ‘+@tableName+’ WHERE ‘+@condition+’
AND ‘+@pkColumn+@strTemp+'(‘+@pkColumn+’)’+’ FROM (SELECT TOP
‘+STR((@currentPage-1)*@pageSize)+’ ‘+@pkColumn+’ FROM
‘+@tableName+’where’+@condition+@strOrderType+’) AS
TabTemp)’+@strOrderTypeELSESET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+’
‘+@columns+’ FROM ‘+@tableName+’ WHERE
‘+@pkColumn+@strTemp+'(‘+@pkColumn+’)’+’ FROM (SELECT TOP
‘+STR((@currentPage-1)*@pageSize)+’ ‘+@pkColumn+’ FROM
‘+@tableName+@strOrderType+’) AS TabTemp)’+@strOrderTypeENDENDEXEC
(@strSql)END–存储过程结束–分页得到客房信息列表测试EXEC
cndoup_GetPageOfRecords 20,2,’房间号=RoomNum,房间状态=(SELECT
RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID),房间状态=(SELECT RSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID),床位数=BedNum,楼层=Floors,描述=RoomDes,备注=RoomRemark’,’Room’,”,’RoomID’,0,’RoomID’–根据房间号得到客房信息测试EXEC
cndoup_GetPageOfRecords 1,1,’房间号=RoomNum,房间状态=(SELECT
RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID),房间状态=(SELECT RSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID),BedNum,Floors,RoomDes,RoomRemark’,’Room’,’RoomNum=304′,’RoomID’,0,’RoomID’

ELSE — 其他页
BEGIN
IF @condition !=”
SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+’ '+@columns+' FROM
'+@tableName+
‘ WHERE '+@condition+' AND '+@pkColumn+@strTemp+'('+@pkColumn+')'+'
FROM (SELECT TOP ‘+STR((@currentPage-1)*@pageSize)+
'+@pkColumn+' FROM '+@tableName+@strOrderType+') AS
TabTemp)’+@strOrderType
ELSE
SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+’ '+@columns+' FROM
'+@tableName+
‘ WHERE '+@pkColumn+@strTemp+'('+@pkColumn+')'+' FROM (SELECT TOP
‘+STR((@currentPage-1)*@pageSize)+’ '+@pkColumn+
‘ FROM '+@tableName+@strOrderType+') AS TabTemp)’+@strOrderType
END

复制代码 代码如下:–使用说明
本代码适用于MsSql2000,对于其它数据库也可用.但没必要 –创建存储过程
CREATE PROCEDURE pagination @tblName varchar(255), — 表名 @strGetFields
varchar(1000) = ‘*’, — 需要返回的列 @fldName varchar(255)=”, —
排序的字段名(可包含如TABLE.FLDNAME形式) @PageSize int = 10, — 页尺寸
@PageIndex int = 1, — 页码 @doCount bit = 0, — 返回记录总数, 非 0
值则返回 @OrderType bit = 0, — 设置排序类型, 非 0 值则降序 @strWhere
varchar(1500) = ” — 查询条件 (注意: 不要加 where) AS declare @strSQL
varchar(5000) — 主语句 declare @strTmp varchar(110) — 临时变量 declare
@strOrder varchar(400) — 排序类型 declare @fldName_t varchar(255) —
在分页时用的排序字段名,不包含多表并列时的表名 set @fldName_t =
right(@fldName,len(@fldName)-CHARINDEX(‘.’,@fldName)) if @doCount != 0
begin if @strWhere !=” set @strSQL = ‘select count(*) as Total from ‘

— 计算出总页数 IF @intTotoRecords%@intPageSize 0 set @intTotoPages =
cast(@intTotoRecords/@intPageSize as int) + 1; ELSE set
@intTotoPages=cast(@intTotoRecords/@intPageSize as int) ;

AS
BEGIN                                          –存储过程开始
DECLARE @strTemp varchar(300)
DECLARE @strSql varchar(5000)              –该存储过程最后执行的语句
DECLARE @strOrderType varchar(1000)        –排序类型语句 (order by
column asc或者order by column desc)

  • @tblName + ‘ where ‘+@strWhere else set @strSQL = ‘select count(*) as
    Total from ‘ + @tblName + ” end
    –以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
    else begin if @OrderType != 0 begin set @strTmp = ‘(select min’ set
    @strOrder = ‘ order by ‘ + @fldName +’ desc’
    –如果@OrderType不是0,就执行降序,这句很重要! end else begin set
    @strTmp = ‘(select max’ set @strOrder = ‘ order by ‘ + @fldName +’ asc’
    end if @PageIndex = 1 begin if @strWhere != ” set @strSQL = ‘select top
    ‘ + str(@PageSize) +’ ‘+@strGetFields+ ‘ from ‘ + @tblName + ‘ where ‘ +
    @strWhere + ‘ ‘ + @strOrder else set @strSQL = ‘select top ‘ +
    str(@PageSize) +’ ‘+@strGetFields+ ‘ from ‘+ @tblName + ‘ ‘+ @strOrder
    –如果是第一页就执行以上代码,这样会加快执行速度 end else begin
    –以下代码赋予了@strSQL以真正执行的SQL代码 set @strSQL = ‘select top ‘ +
    str(@PageSize) +’ ‘+@strGetFields+ ‘ from ‘+ @tblName + ‘ where ‘ +
    @fldName + ‘ ‘ + @strTmp + ‘ (‘+ @fldName_t + ‘) from (select top ‘ +
    str((@PageIndex-1)*@PageSize) + ‘ ‘+ @fldName + ‘ from ‘ + @tblName +
    ” + @strOrder + ‘) as tblTmp)’+ @strOrder if @strWhere != ” set
    @strSQL = ‘select top ‘ + str(@PageSize) +’ ‘+@strGetFields+ ‘ from ‘+
    @tblName + ‘ where ‘ + @fldName + ‘ ‘ + @strTmp + ‘ (‘+ @fldName_t + ‘)
    from (select top ‘ + str((@PageIndex-1)*@PageSize) + ‘ ‘+ @fldName + ‘
    from ‘ + @tblName + ‘ where ‘ + @strWhere + ‘ ‘+ @strOrder + ‘) as
    tblTmp) and ‘ + @strWhere + ‘ ‘ + @strOrder end end exec (@strSQL) go
    –测试 create table news –建表 ( n_id int iDENTITY(1,1) primary key,
    n_title char(200), n_content text ) –写循环插入1000000条的数据 create
    proc tt as declare @i int set @i=0 while(@i1000000) begin insert into
    news(n_title,n_content) values(‘sb’,’dsfsdfsd’) set @i=@i+1 end exec
    tt exec pagination ‘news’,’*’,’n_id’,1000,2,0,0,” 第二篇复制代码 代码如下:自己改写的一个分页存储过程
    CREATE PROC Paging ( @pageSize int, @pageIndex int, @pageField
    nvarchar(32), @countTotal bit=1, @fieldQuery nvarchar(512), @tableQuery
    nvarchar(512), @whereQuery nvarchar(2048), @orderQuery nvarchar(512) )
    AS DECLARE @bdate Datetime SET @bdate = getdate() DECLARE @itemcount int
    SET @itemcount=@pageIndex*@pageSize DECLARE @itemlowwer int SET
    @itemlowwer=(@pageIndex-1)*@pageSize DECLARE @cmd nvarchar(3062) IF
    @pageIndex=1 SET @cmd =’SELECT TOP ‘+CAST(@pageSize AS NVARCHAR)+’
    ‘+@fieldQuery+’ FROM ‘+@tableQuery+’ WHERE ‘+@whereQuery+’ ORDER BY
    ‘+@orderQuery ELSE SET @cmd=’SELECT ‘+@fieldQuery+’ FROM ‘+@tableQuery+’
    WHERE ‘+@pageField+’ IN (SELECT TOP ‘+CAST(@itemcount as nvarchar)+’
    ‘+@pageField+’ FROM ‘+@tableQuery+’ WHERE ‘+@whereQuery+’ ORDER BY ‘+
    @orderQuery+’) AND ‘+@pageField+’ NOT IN (SELECT TOP ‘ +CAST(@itemlowwer
    as nvarchar)+’ ‘+@pageField+’ FROM ‘+@tableQuery+’ WHERE ‘+@whereQuery+’
    ORDER BY ‘+ @orderQuery+’)’ –print @cmd EXEC(@cmd) SELECT DATEDIFF( ms ,
    @bdate , getdate() ) IF @countTotal =1 BEGIN SET @cmd = ‘SELECT COUNT(
    0) FROM ‘+@tableQuery+’ WHERE ‘+@whereQuery EXEC(@cmd) END GO

/****** 对象: StoredProcedure [dbo].[P_viewPage] 脚本日期:
05/14/2012 08:49:34 ******/SET ANSI_NULLS ONGOSET
QUOTED_IDENTIFIER ONGO

BEGIN
IF @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

文章也是从朋友那里抄来的两个不错的sql分页存储过程实现,听说效率比较高的一个存储过程,有需要的同学可以了解一下或提供更好的解决方案。

/*测试*/
–分页得到客房信息列表测试
EXEC cndoup_GetPageOfRecords 20,2,’房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID),
房间状态=(SELECT RSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID),
床位数=BedNum,
楼层=Floors,
描述=RoomDes,
备注=RoomRemark’,’Room’,”,’RoomID’,0,’RoomID’

BEGIN

–· 得到客房类型信息列表测试
EXEC cndoup_GetPageOfRecords
10,1,’RoomTypeDes,Price,Area,AddBed,MaxBedNum,BedPrice,HourRoom,HourPrice,Remark’,’RoomType’,”,’RoomTypeID’,0,’RoomTypeID’

DECLARE @strSQL nvarchar(4000) DECLARE @intCurPage int DECLARE @strWhere
varchar(200) DECLARE @setvalue_error int

–存储过程结束

代码如下复制代码

–根据房间号得到客房信息测试
EXEC cndoup_GetPageOfRecords 1,1,’房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID),
房间状态=(SELECT RSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID),
BedNum,
Floors,
RoomDes,
RoomRemark’,’Room’,’RoomNum=304′,’RoomID’,0,’RoomID’

set @strWhere = ”; — Where 语句

www.9778.com,END
EXEC (@strSql)
END

IF @intPageSize=0 GOTO errPageSize; — 返回错误:每页记录数范围错误

IF @currentPage = 1 –第一页
BEGIN
IF @condition != ”
SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+’ '+@columns+' FROM
'+@tableName+
‘ WHERE '+@condition+@strOrderType
ELSE
SET @strSql = ‘SELECT TOP ‘+STR(@pageSize)+’ '+@columns+' FROM
'+@tableName+@strOrderType
END

–输出内容SELECT @TotalCount as N’@TotalCount’, @TotalPageCount as
N’@TotalPageCount’

/*通用分页存储过程*/
USE HotelManagementSystem
GO
IF EXISTS(SELECT * FROM sys.objects WHERE
NAME=’cndoup_GetPageOfRecords’)
DROP PROCEDURE cndoup_GetPageOfRecords
GO
–创建存储过程
CREATE PROCEDURE cndoup_GetPageOfRecords
@pageSize int = 20,                        –分页大小
@currentPage int ,                        –第几页
@columns varchar(1000) = ‘*’,              –需要得到的字段
@tableName varchar(100),                  –需要查询的表 
@condition varchar(1000) = ”,            –查询条件,
不用加where关键字
@ascColumn varchar(100) = ”,              –排序的字段名 (即 order by
column asc/desc)
@bitOrderType bit = 0,                    –排序的类型
(0为升序,1为降序)
@pkColumn varchar(50) = ”                –主键名称

DoNext: IF @intTotoRecords0 GOTO errTotoRecords; —
返回错误:记录总数错误

create PROC [dbo].[P_viewPage]– Add the parameters for the stored
procedure here@TableName VARCHAR(200), –表名@FieldList VARCHAR(2000),
–显示列名,如果是全部字段则为*@PrimaryKey VARCHAR(100),
–单一主键或唯一值键@Where VARCHAR(8000),
–查询条件不含’where’字符,如id10 and len(userid)9@Order VARCHAR(1000),
–排序不含’order by’字符,如id asc,userid
desc,必须指定asc或desc–注意当@SortType=3时生效,记住一定要在最后加上主键,否则会让你比较郁闷@SortType
INT, –排序规则1:正序asc 2:倒序desc 3:多列排序方法@RecorderCount INT,
–记录总数0:会返回总记录@PageSize INT, –每页输出的记录数@PageIndex INT,
–当前页数@TotalCount INT OUTPUT, –记返回总记录@TotalPageCount INT
OUTPUT –返回总页数AS– SET NOCOUNT ON added to prevent extra result
sets from– interfering with SELECT statements.SET NOCOUNT ONIF
ISNULL(@TotalCount,”) = ” SET @TotalCount = 0SET @Order =
RTRIM(LTRIM(@Order))SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))SET
@FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),’ ‘,”)WHILE CHARINDEX(‘,
‘,@Order) 0 OR CHARINDEX(‘ ,’,@Order) 0BEGINSET @Order =
REPLACE(@Order,’, ‘,’,’)SET @Order = REPLACE(@Order,’ ,’,’,’)ENDIF
ISNULL(@TableName,”) = ” OR ISNULL(@FieldList,”) = ”OR
ISNULL(@PrimaryKey,”) = ”OR @SortType 1 OR @SortType 3OR
@RecorderCount 0 OR @PageSize 0 OR @PageIndex 0BEGIN
PRINT(‘ERR_00参数错误’) RETURNENDIF @SortType = 3BEGINIF
(UPPER(RIGHT(@Order,4))!=’ ASC’ AND UPPER(RIGHT(@Order,5))!=’
DESC’)BEGIN PRINT(‘ERR_02排序错误’) RETURN ENDENDDECLARE @new_where1
VARCHAR(8000)DECLARE @new_where2 VARCHAR(8000)DECLARE @new_order1
VARCHAR(1000)DECLARE @new_order2 VARCHAR(1000)DECLARE @new_order3
VARCHAR(1000)DECLARE @Sql VARCHAR(8000)DECLARE @SqlCount
NVARCHAR(4000)IF ISNULL(@where,”) = ”BEGINSET @new_where1 = ‘ ‘SET
@new_where2 = ‘ WHERE ‘ENDELSEBEGINSET @new_where1 = ‘ WHERE ‘ +
@whereSET @new_where2 = ‘ WHERE ‘ + @where + ‘ AND ‘ENDIF
ISNULL(@order,”) = ” OR @SortType = 1 OR @SortType = 2BEGINIF
@SortType = 1 BEGIN SET @new_order1 = ‘ ORDER BY ‘ + @PrimaryKey + ‘
ASC’ SET @new_order2 = ‘ ORDER BY ‘ + @PrimaryKey + ‘ DESC’ ENDIF
@SortType = 2 BEGIN SET @new_order1 = ‘ ORDER BY ‘ + @PrimaryKey + ‘
DESC’ SET @new_order2 = ‘ ORDER BY ‘ + @PrimaryKey + ‘ ASC’
ENDENDELSEBEGINSET @new_order1 = ‘ ORDER BY ‘ + @OrderENDIF @SortType =
3 AND CHARINDEX(‘,’+@PrimaryKey+’ ‘,’,’+@Order)0BEGINSET @new_order1 =
‘ ORDER BY ‘ + @OrderSET @new_order2 = @Order + ‘,’SET @new_order2 =
REPLACE(REPLACE(@new_order2,’ASC,’,'{ASC},’),’DESC,’,'{DESC},’)SET
@new_order2 =
REPLACE(REPLACE(@new_order2,'{ASC},’,’DESC,’),'{DESC},’,’ASC,’)SET
@new_order2 = ‘ ORDER BY ‘ +
SUBSTRING(@new_order2,1,LEN(@new_order2)-1)IF @FieldList ‘*’ BEGIN
SET @new_order3 = REPLACE(REPLACE(@Order + ‘,’,’ASC,’,’,’),’DESC,’,’,’)
SET @FieldList = ‘,’ + @FieldList WHILE CHARINDEX(‘,’,@new_order3)0
BEGIN IF
CHARINDEX(SUBSTRING(‘,’+@new_order3,1,CHARINDEX(‘,’,@new_order3)),’,’+@FieldList+’,’)0
BEGIN SET @FieldList = @FieldList + ‘,’ +
SUBSTRING(@new_order3,1,CHARINDEX(‘,’,@new_order3)) END SET
@new_order3 =
SUBSTRING(@new_order3,CHARINDEX(‘,’,@new_order3)+1,LEN(@new_order3))
END SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList)) ENDENDSET
@SqlCount = ‘SELECT
@TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/’+
CAST(@PageSize AS VARCHAR)+’) FROM ‘ + @TableName + @new_where1IF
@RecorderCount = 0BEGINEXEC SP_EXECUTESQL @SqlCount,N’@TotalCount INT
OUTPUT,@TotalPageCount INT OUTPUT’,@TotalCount OUTPUT,@TotalPageCount
OUTPUTENDELSEBEGINSELECT @TotalCount = @RecorderCountENDIF @PageIndex
CEILING((@TotalCount+0.0)/@PageSize)BEGINSET @PageIndex =
CEILING((@TotalCount+0.0)/@PageSize)ENDIF @PageIndex = 1 OR @PageIndex =
CEILING((@TotalCount+0.0)/@PageSize)BEGINIF @PageIndex = 1
–返回第一页数据 BEGIN SET @Sql = ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ +
@FieldList + ‘ FROM ‘ + @TableName + @new_where1 + @new_order1 ENDIF
@PageIndex = CEILING((@TotalCount+0.0)/@PageSize) –返回最后一页数据
BEGIN SET @Sql = ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘
FROM (‘ + ‘SELECT TOP ‘ +
STR(ABS(@PageSize*@PageIndex-@TotalCount-@PageSize)) + ‘ ‘ + @FieldList

SET @strSQL = ‘SELECT COUNT(1) FROM ‘ + @strTableList + ‘ ‘ + @strWhere;

— 确定待查询的页码数 — 如果页码数小于等于 0 ,则查询返回第一页 —
如果页码数大于最大页码数,则查询返回最后一页 IF @intCurrentPage=0 set
@intCurPage=1; ELSE IF @intCurrentPage@intTotoPages set @intCurPage =
@intTotoPages; ELSE set @intCurPage=@intCurrentPage;

再分享一个,一个比较创新的存储过程:(注:此存储过程利用了sql2005函数,适用于sql20005极其以上版本)

/****** 对象: StoredProcedure [dbo].[QueryPagination]
脚本日期: 05/14/2012 08:57:04 ******/SET ANSI_NULLS ONGOSET
QUOTED_IDENTIFIER ONGO

OPEN cur_t FETCH NEXT FROM cur_t into @intTotoRecords while
@@fetch_status = 0 begin fetch next from cur_t into @intTotoRecords
end close cur_t deallocate cur_t

errPageSize: return -1;

errTotoRecords: return -2;

exec(‘DECLARE cur_t CURSOR FOR ‘+ @strSQL)

这个还觉得不错的一个存储过程

Set xact_abort onBegin Tran

If @@error0 return @@error; Else return 0;

/*————————————————-* strFieldList
字段列表* strTableList 查询表列表* strWhereClause 查询条件*
strOrderFld 排序字段* intTotoRecords (输入/输出)总记录数* intTotoPages
(输入/输出)总页数* intPageSize 每页记录数* intCurrentPage 当前页*
intCountToto 是否统计总数* 0: 不容积总数* 1:
统计总数————————————————-*/CREATE
PROCEDURE [dbo].[QueryPagination] @t char(1)=”, @strFieldList
varchar(1000)=”, @strTableList varchar(300)=”, @strWhereClause
varchar(1000)=”, @strOrderFld varchar(100)=”, @intCurrentPage int=1,
@intPageSize int=10, @intCountToto int=0, @intTotoRecords int=0 out,
@intTotoPages int=0 outAS

代码如下复制代码

If @strWhere ” Set @strSQL = ‘SELECT * FROM (SELECT ROW_NUMBER() OVER
(ORDER BY ‘ + @strOrderFld + ‘) ROWNUM,’ + @strFieldList + ‘ FROM ‘ +
@strTableList + ‘ ‘ + @strWhere + ‘) TAB_TMP WHERE ROWNUM’ +
cast((@intCurPage-1)*@intPageSize as varchar) + ‘ and ROWNUM ‘ +
cast(@intCurPage*@intPageSize+1 as varchar); Else Set @strSQL = ‘SELECT
* FROM (SELECT ROW_NUMBER() OVER (ORDER BY ‘ + @strOrderFld + ‘)
ROWNUM,’ + @strFieldList + ‘ FROM ‘ + @strTableList + ‘) TAB_TMP WHERE
ROWNUM’ + cast((@intCurPage-1)*@intPageSize as varchar) + ‘ and ROWNUM
‘ + cast(@intCurPage*@intPageSize+1 as varchar) ; Execute(@strSQL);

If @strWhereClause ” set @strWhere = @strWhereClause;

END

  • ‘ FROM ‘ + @TableName + @new_where1 + @new_order2 + ‘ ) AS TMP ‘ +
    @new_order1 ENDENDELSEBEGINIF @SortType = 1 –仅主键正序排序 BEGIN IF
    @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)/2 –正向检索 BEGIN SET
    @Sql = ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ‘ +
    @TableName + @new_where2 + @PrimaryKey + ‘ ‘ + ‘(SELECT MAX(‘ +
    @PrimaryKey + ‘) FROM (SELECT TOP ‘ + STR(@PageSize*(@PageIndex-1)) + ‘
    ‘ + @PrimaryKey + ‘ FROM ‘ + @TableName + @new_where1 + @new_order1 +’
    ) AS TMP) ‘+ @new_order1 END ELSE –反向检索 BEGIN SET @Sql = ‘SELECT
    TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM (‘ + ‘SELECT TOP ‘ +
    STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ‘ + @TableName + @new_where2
  • @PrimaryKey + ‘ ‘ + ‘(SELECT MIN(‘ + @PrimaryKey + ‘) FROM (SELECT TOP
    ‘ + STR(@TotalCount-@PageSize*@PageIndex) + ‘ ‘ + @PrimaryKey + ‘ FROM
    ‘ + @TableName + @new_where1 + @new_order2 +’ ) AS TMP) ‘+
    @new_order2 + ‘ ) AS TMP ‘ + @new_order1 END ENDIF @SortType = 2
    –仅主键反序排序 BEGIN IF @PageIndex =
    CEILING((@TotalCount+0.0)/@PageSize)/2 –正向检索 BEGIN SET @Sql =
    ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ‘ +
    @TableName + @new_where2 + @PrimaryKey + ‘ ‘ + ‘(SELECT MIN(‘ +
    @PrimaryKey + ‘) FROM (SELECT TOP ‘ + STR(@PageSize*(@PageIndex-1)) + ‘
    ‘ + @PrimaryKey +’ FROM ‘+ @TableName + @new_where1 + @new_order1 + ‘)
    AS TMP) ‘+ @new_order1 END ELSE –反向检索 BEGIN SET @Sql = ‘SELECT TOP
    ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM (‘ + ‘SELECT TOP ‘ +
    STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ‘ + @TableName + @new_where2
  • @PrimaryKey + ‘ ‘ + ‘(SELECT MAX(‘ + @PrimaryKey + ‘) FROM (SELECT TOP
    ‘ + STR(@TotalCount-@PageSize*@PageIndex) + ‘ ‘ + @PrimaryKey + ‘ FROM
    ‘ + @TableName + @new_where1 + @new_order2 +’ ) AS TMP) ‘+
    @new_order2 + ‘ ) AS TMP ‘ + @new_order1 END ENDIF @SortType = 3
    –多列排序,必须包含主键,且放置最后,否则不处理 BEGIN IF CHARINDEX(‘,’
  • @PrimaryKey + ‘ ‘,’,’ + @Order) = 0 BEGIN PRINT(‘ERR_02’) RETURN END
    IF @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)/2 –正向检索 BEGIN
    SET @Sql = ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM (
    ‘ + ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ( ‘ + ‘
    SELECT TOP ‘ + STR(@PageSize*@PageIndex) + ‘ ‘ + @FieldList + ‘ FROM ‘
  • @TableName + @new_where1 + @new_order1 + ‘ ) AS TMP ‘ + @new_order2
  • ‘ ) AS TMP ‘ + @new_order1 END ELSE –反向检索 BEGIN SET @Sql =
    ‘SELECT TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ( ‘ + ‘SELECT
    TOP ‘ + STR(@PageSize) + ‘ ‘ + @FieldList + ‘ FROM ( ‘ + ‘ SELECT TOP ‘
  • STR(@TotalCount-@PageSize *@PageIndex+@PageSize) + ‘ ‘ + @FieldList +
    ‘ FROM ‘ + @TableName + @new_where1 + @new_order2 + ‘ ) AS TMP ‘ +
    @new_order1 + ‘ ) AS TMP ‘ + @new_order1 END ENDENDEXEC(@Sql)

Set @setvalue_error = @@errorIf @setvalue_error0 Begin Set
@intTotoRecords = -1; GOTO DoNext; Rollback Tran End Else Begin Commit
Tran GOTO DoNext; End