一篇好用的通用分页存款和储蓄进度,能够用在此外付出上啊,只要小小的校勘就好了,有亟待的对象能够参谋一下本款实例。
代码如下复制代码 /*通用分页存款和储蓄过程*/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/descState of Qatar@bitOrderType bit = 0, –排序的品类
(0为升序,1为降序State of Qatar@pkColumn varchar(50卡塔尔 = ” –主键名称ASBEGIN
–存款和储蓄进度最初DECLARE @strTemp varchar(300卡塔尔DECLARE @strSql varchar(5000卡塔尔–该存款和储蓄进度最后实行的语句DECLARE @strOrderType varchar(1000卡塔尔国–排序类型语句 (order by column asc只怕order by column descState of QatarBEGINIF
@bitOrderType = 1 –降序BEGINSET @strOrderType = ‘ OEscortDE中华V BY
‘+@ascColumn+’ DESC’SET @strTemp = ‘(SELECT min’ENDELSE–升序BEGINSET
@strOrderType = ‘ OEscortDEWrangler 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+’State of Qatar AS TabTemp卡塔尔国’+@strOrderTypeENDENDEXEC
(@strSqlState of QatarEND–存款和储蓄进度截止–分页得到客房音信列表测验EXEC
cndoup_GetPageOfRecords 20,2,’房间号=RoomNum,房间状态=(SELECT
RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID卡塔尔(قطر‎,房间状态=(SELECT PAJEROSDec 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 RAV4SDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusIDState of Qatar,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

  • @tblName + ‘ where ‘+@strWhere else set @strSQL = ‘select count(*State of Qatar 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(@i1000000State of Qatar 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, @pageFieldnvarchar(32卡塔尔, @countTotal bit=1, @田野先生Query 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

BEGIN

–依照房间号获得客房新闻测验
EXEC cndoup_GetPageOfRecords 1,1,’房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID卡塔尔国,
房子状态=(SELECT MuranoSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID卡塔尔(قطر‎,
BedNum,
Floors,
RoomDes,
RoomRemark’,’Room’,’RoomNum=304′,’RoomID’,0,’RoomID’

复制代码 代码如下:–使用验证
本代码适用于MsSql二〇〇四,对于其余数据库也可用.但没要求 –成立存款和储蓄进程CREATE PROCEDURE pagination @tblName varchar(255卡塔尔(قطر‎, — 表名 @strGetFields
varchar(1000卡塔尔 = ‘*’, — 须要回到的列 @fldName varchar(255State of Qatar=”, —
排序的字段名(可含蓄如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(110State of Qatar — 不经常变量 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 @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);

/*金沙糖果派对2015cc,测试*/
–分页获得客房音信列表测验
EXEC cndoup_GetPageOfRecords 20,2,’房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID卡塔尔,
房屋状态=(SELECT PRADOSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID卡塔尔国,
床位数=BedNum,
楼层=Floors,
描述=RoomDes,
备注=RoomRemark’,’Room’,”,’RoomID’,0,’RoomID’

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

–存款和储蓄进度结束

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

–· 获得客房类型音讯列表测验
EXEC cndoup_GetPageOfRecords
10,1,’RoomTypeDes,Price,Area,AddBed,MaxBedNum,BedPrice,HourRoom,HourPrice,Remark’,’RoomType’,”,’RoomTypeID’,0,’RoomTypeID’

  • ‘ FROM ‘ + @TableName + @new_where1 + @new_order2 + ‘ ) AS TMP ‘ +
    @new_order1 ENDENDELSEBEGINIF @SortType = 1 –仅主键正序排序 BEGIN IF
    @PageIndex = CEILING((@TotalCount+0.0卡塔尔/@PageSizeState of Qatar/2 –正向检索 BEGIN SET
    @Sql = ‘SELECT TOP ‘ + STHighlander(@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 ‘ + STENVISION(@PageSize卡塔尔 + ‘ ‘ + @FieldList + ‘ FROM (‘ + ‘SELECT TOP ‘ +
    ST酷路泽(@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_金沙糖果派对网站app,order2 + ‘ ) AS TMP ‘ + @new_order1 END ENDIF @SortType = 2
    –仅主键反序排序 BEGIN IF @PageIndex =
    CEILING((@TotalCount+0.0卡塔尔(قطر‎/@PageSizeState of Qatar/2 –正向检索 BEGIN SET @Sql =
    ‘SELECT TOP ‘ + ST科雷傲(@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
    ‘ + STLAND(@PageSizeState of Qatar + ‘ ‘ + @FieldList + ‘ FROM (‘ + ‘SELECT TOP ‘ +
    STSportage(@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 CHA奥迪Q3INDEX(‘,’
  • @PrimaryKey + ‘ ‘,’,’ + @Order) = 0 BEGIN PRINT(‘ERR_02’卡塔尔国 RETUHavalN END
    IF @PageIndex = CEILING((@TotalCount+0.0卡塔尔/@PageSize卡塔尔/2 –正向检索 BEGIN
    SET @Sql = ‘SELECT TOP ‘ + STHighlander(@PageSize卡塔尔国 + ‘ ‘ + @FieldList + ‘ FROM (
    ‘ + ‘SELECT TOP ‘ + ST路虎极光(@PageSize卡塔尔 + ‘ ‘ + @菲尔德List + ‘ FROM ( ‘ + ‘
    SELECT TOP ‘ + STHaval(@PageSize*@PageIndex) + ‘ ‘ + @FieldList + ‘ FROM ‘
  • @TableName + @new_where1 + @new_order1 + ‘ ) AS TMP ‘ + @new_order2
  • ‘ ) AS TMP ‘ + @new_order1 END ELSE –反向找出 BEGIN SET @Sql =
    ‘SELECT TOP ‘ + ST奥迪Q5(@PageSize卡塔尔国 + ‘ ‘ + @FieldList + ‘ FROM ( ‘ + ‘SELECT
    TOP ‘ + STLAND(@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)

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

— 分明待查询的页码数 — 纵然页码数小于等于 0 ,则查询重返第一页 —
若是页码数大于最大页码数,则查询重返最终一页 IF @intCurrentPage=0 set
@intCurPage=1; ELSE IF @intCurrentPage@intTotoPages set @intCurPage =
@intTotoPages; ELSE set @intCurPage=@intCurrentPage;

/*通用分页存款和储蓄进度*/
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为降序State of Qatar
@pkColumn varchar(50卡塔尔国 = ”                –主键名称

这一个还感到不错的三个囤积进程

END
EXEC (@strSql)
END

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

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注