通用分页存款和储蓄进度实例,收罗多篇

一篇好用的通用分页存储过程,可以用在任何开发上哦,只要小小的改动就好了,有需要的朋友可以参考一下本款实例。
代码如下复制代码 /*通用分页存储过程*/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’

/*通用分页存储过程*/
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) = ”                –主键名称

复制代码 代码如下:–使用说明
本代码适用于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 ‘

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

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

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

发表评论

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