Menu
Woocommerce Menu

通用分页存储过程3522vip靠谱吗:,文章也是从朋友那里抄来的两个不错的sql分页存储过程实现

0 Comment

一篇好用的通用分页存款和储蓄进度,能够用在此外付出上啊,只要小小的退换就好了,有亟待的相爱的人能够参照一下本款实例。
代码如下复制代码 /*通用分页存款和储蓄进度*/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为降序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 desc卡塔尔(قطر‎BEGINIF
@bitOrderType = 1 –降序BEGINSET @strOrderType = ‘ O卡宴DEMurano BY
‘+@ascColumn+’ DESC’SET @strTemp = ‘(SELECT min’ENDELSE–升序BEGINSET
@strOrderType = ‘ O中华VDE宝马X5 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 HavalSDec 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.RoomTypeIDState of Qatar,房间状态=(SELECT 智跑SDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusIDState of Qatar,BedNum,Floors,RoomDes,RoomRemark’,’Room’,’RoomNum=304′,’RoomID’,0,’RoomID’

–依据房间号获得客房信息测量试验
EXEC cndoup_GetPageOfRecords 1,1,’房间号=RoomNum,
房间状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID卡塔尔,
屋企状态=(SELECT ENCORESDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID卡塔尔,
BedNum,
Floors,
RoomDes,
RoomRemark’,’Room’,’RoomNum=304′,’RoomID’,0,’RoomID’

  • @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(@PageSizeState of Qatar +’ ‘+@strGetFields+ ‘ from ‘+ @tblName + ‘ ‘+ @strOrder
    –假设是首先页就施行以上代码,那样会加速施行进程 end else begin
    –以下代码授予了@strSQL以真正进行的SQL代码 set @strSQL = ‘select top ‘ +
    str(@PageSizeState of Qatar +’ ‘+@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, @pageFieldnvarchar(32State of Qatar, @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

DoNext: IF @intTotoRecords0 GOTO errTotoRecords; —
重回错误:记录总量错误

 –分组语句
 SET @groupBy = ‘ GROUP BY USerID ‘
 
 SET @RowStartIndex = ( @pageIndex -1) * @pageSize + 1
 SET @RowEndIndex =  @pageIndex  * @pageSize ;
 
 –查询结果 
 SET @sql = ‘SET NOCOUNT ON;
 WITH ResultTable AS ( ‘ + @select + @from + @condition +’)
 SELECT * FROM ResultTable WHERE RowNumber between ‘ +
 Cast(@RowStartIndex AS VARCHAR(32)) + ‘ AND ‘ + CAST(@RowEndIndex AS
VARCHAR(32))
 + ‘  ; SELECT  count(*) as totalcount  ‘  + @from + @condition + ‘
 ‘
 ;
 –PRINT @sql;
 EXEC(@sql); 
 
END

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

复制代码 代码如下:–使用验证
本代码适用于MsSql二〇〇三,对于其他数据库也可用.但没供给 –创设存款和储蓄进度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(400State of Qatar — 排序类型 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 ‘

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

BEGIN
 SET NOCOUNT on
 IF @orderBy <> ”
  Set @orderBy = ‘ ORDER BY  ‘  + @orderBy;
 else
  Set @orderBy = ‘ ORDER BY Userid ‘ ;

–存款和储蓄进程停止

BEGIN

 –设置标准语句
 SET @condition = ‘  WHERE 1=1 ‘;
 SET @condition = @condition  + ‘AND userid > 0’;

/*通用分页存款和储蓄进度*/
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卡塔尔 = ”                –主键名称

errPageSize: return -1;

 –设置排序语句
 SET @RowNumber =’ROW_NUMBER() OVER (‘ + @orderBy + ‘ ) as RowNumber
‘; 
 SET @select = @select  + @RowNumber;
 
   SET @from = ‘ FROM users  ‘;

/*测试*/
–分页取得客房新闻列表测量试验
EXEC cndoup_GetPageOfRecords 20,2,’房间号=RoomNum,
房子状态=(SELECT RoomTypeDes FROM RoomType WHERE
RoomTypeID=Room.RoomTypeID卡塔尔(قطر‎,
房间状态=(SELECT EnclaveSDec FROM RoomStatus WHERE
RoomStatusID=Room.RoomStatusID卡塔尔(قطر‎,
床位数=BedNum,
楼层=Floors,
描述=RoomDes,
备注=RoomRemark’,’Room’,”,’RoomID’,0,’RoomID’

/*————————————————-* 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

 SET @select = ‘ select userid,username ,’;

AS
BEGIN                                          –存款和储蓄进程在这里早先
DECLARE @strTemp varchar(300)
DECLARE @strSql varchar(5000State of Qatar              –该存款和储蓄进度最后实践的言语
DECLARE @strOrderType varchar(1000State of Qatar        –排序类型语句 (order by
column asc恐怕order by column desc卡塔尔国

If @strWhereClause ” set @strWhere = @strWhereClause;

–分页存款和储蓄进度示例
Alter PROCEDURE [dbo].[JH_PageDemo]
 @pageSize int = 9000000000,
 @pageIndex int = 1 ,
 @orderBy   Nvarchar(200) = ” — 不加order By
AS
SET NOCOUNT ON
–申明变量
DECLARE @select   VARCHAR(3048);
DECLARE @from       VARCHAR(512);
DECLARE @RowNumber   VARCHAR(256);
DECLARE @condition  nVARCHAR(3990);
DECLARE @groupBy varchar(50);
DECLARE @sql    VARCHAR(3998);
DECLARE @RowStartIndex INT;
DECLARE @RowEndIndex INT;

发表评论

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

相关文章

网站地图xml地图