网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > SQL语法 > 正文

Sqlserver2008分页存储过程

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

【网学网提醒】:网学会员为广大网友收集整理了,Sqlserver2008分页存储过程,希望对大家有所帮助!


    USE[Northwind_cn]
    GO
    /******Object:StoredProcedure[dbo].[PS_GetPageData]ScriptDate:04/21/201202:26:52******/
    SETANSI_NULLSON
    GO
    SETQUOTED_IDENTIFIERON
    GO
    --=============================================
    --Author:
    --Createdate:
    --Description:
    --=============================================
    ALTERPROCEDURE[dbo].[PS_GetPageData]
    --Addtheparametersforthestoredprocedurehere
    @TVNamenvarchar(1000),--表或视图或查询语句
    @IsSelectSQLbit,--@TVName是否带select的查询语句
    @IsReturnRecordbit,--是否返回记录
    @PageSizeint,--每页记录大小
    @PageIndexint,--要返回的页数
    @WhereStrnvarchar(1000),--where后面的查询条件
    @OrderStrnvarchar(400),---orderby后面用于排序的子句
    @FieldStrnvarchar(1000),--要返回的字段名
    @AllRecordCountintoutput,--返回本次查询能够查到的所有记录数目
    @RecordCountintoutput,--本次查到返回的记录数目
    @PageCountintoutput,--返回本次查询能够返回的页数
    @RMessagenvarchar(max)output--返回执行结果信息
    AS
    BEGIN
    --SETNOCOUNTONaddedtopreventextraresultsetsfrom
    --interferingwithSELECTstatements.
    SETNOCOUNTON;
    declare@SQLCountnvarchar(max);
    declare@SQLSelectnvarchar(max);
    declare@SQLstrnvarchar(max);
    declare@SQLOrderStrnvarchar(450);
    if(@IsSelectSQL=1)--表明是一个查询语句
    begin
    set@TVName=N'('+@TVName+')ttb'
    end
    if(RTRIM(LTRIM(@WhereStr))<>'')--没有输入查询条件
    begin
    set@SQLCount='select@AllRecordCount=count(*)from'+@TVName+'where'+@WhereStr
    end
    else
    begin
    set@SQLCount='select@AllRecordCount=count(*)from'+@TVName
    end
    
    --执行可以多次重复使用或动态生成的Transact-SQL语句或批处理。Transact-SQL语句或批处理可以包含嵌入参数。
    execsp_executesql@SQLCount,N'@AllRecordCountintout',@AllRecordCountoutput
    --计算页数
    if(@AllRecordCount%@PageSize>0)
    begin
    set@PageCount=@AllRecordCount/@PageSize+1
    end
    else
    begin
    set@PageCount=@AllRecordCount/@PageSize
    end
    if(@PageIndex>@PageCountor@PageIndex<=0or@PageSize<=0)--要求的页超出总页面范围
    begin
    set@RecordCount=0
    set@RMessage='Outofpageorbadsize'
    return
    end
    
    --计算本次能够返回的行数
    if(@AllRecordCount=0)--如果符合记录数为0
    begin
    set@RecordCount=0
    end
    else
    begin
    if(@AllRecordCount%@PageSize=0)--如果符合记录数能被页数整除
    begin
    set@RecordCount=@PageSize;
    end
    else
    begin
    if(@PageIndex=@PageCount)--如果符合记录数不能能被页数整除,并且是最后一页
    begin
    set@RecordCount=@AllRecordCount%@PageSize;
    end
    else
    be
    gin
    set@RecordCount=@PageSize;--如果不能整除并且不在最后一页,返回页记录数
    end
    end
    end
    if(@IsReturnRecord=0)
    begin
    set@RMessage=@SQLCount
    return
    end
    
    
    if(Rtrim(LTRIM(@OrderStr))<>'')--排序字段为空
    begin
    set@SQLOrderStr=N'orderby'+@OrderStr
    end
    else
    begin
    set@RMessage='NoOrderField'
    return
    end
    declare@StartNumsvarchar(20)
    declare@EndNumsvarchar(20)
    set@StartNums=(@PageSize*(@PageIndex-1))+1
    set@EndNums=(@PageSize*@PageIndex)
    if(null=@FieldStrorRTRIM(LTRIM(@FieldStr))='')--没有输入字段
    begin
    set@FieldStr=N'*'
    end
    
    if(RTRIM(LTRIM(@WhereStr))<>'')--没有输入查询条件
    begin
    set@SQLSelect=@TVName+'where'+@WhereStr
    end
    else
    begin
    set@SQLSelect=@TVName
    end
    
    set@SQLstr='
    witht_rowtableas(selectrow_number()over('+@SQLOrderStr+')asrow_number,'+@FieldStr+'from'+@SQLSelect+')
    select'+@FieldStr+'fromt_rowtablewhererow_numberbetween'+@StartNums+'and'+@EndNums++''+@SQLOrderStr
    set@RMessage=@SQLstr
    exec(@SQLstr)
    END
    
设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师