zoukankan      html  css  js  c++  java
  • 分页存储过程

    CREATE PROCEDURE GetPageRecords
        @tblName      varchar(255),     
        @fldName      varchar(255),      
        @PageSize     int = 12,          
        @PageIndex    int = 1,          
        @IsCount      bit = 0,           
        @OrderType    bit = 0,           
        @strWhere     varchar(1000) = '' 
     /*<summary>************************************************************************************************************
     名称: GetPageRecords
     功能: 获取分页数据每页的数据或者数据的总共条数
     参数:
        input: @tblName     表名
               @fldName     用于排序的字段
        @PageSize    每页显示数据条数
        @PageIndex   页码
        @IsCount     返回记录总数 ,非 0 值则返回记录总数
        @OrderType   设置排序类型,非 0 值则降序
        @strWhere    查询条件(注意:不要加 where)
       ——————————————————————————————————————————————————————————

        @strSql      主语句
        @strTmp      临时变量
        @strOrder    排序类型
     数据来源: Documents 新闻表
     制作人:杨文珺
     日期: 2004/9/21
     注:该存储过程较为通用,若想进一步提高效率,即可提供专门针对该应用的select语句取代 select *
     ************************************************************************************************************</summary>*/
    AS

    declare  @strSQL   varchar(2000)     -- 主语句
    declare @strTmp   varchar(1000)     -- 临时变量
    declare @strOrder varchar(1000)       -- 排序类型

    if @OrderType != 0
    begin
        set @strTmp = "<(select min"
        set @strOrder = " order by [" + @fldName +"] desc"
    end
    else
    begin
        set @strTmp = ">(select max"
        set @strOrder = " order by [" + @fldName +"] asc"
    end

    set @strSQL = "select top " + str(@PageSize) + " * from ["
        + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
        + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
        + @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
        + @strOrder

    if @strWhere != ''
        set @strSQL = "select top " + str(@PageSize) + " * from ["
            + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
            + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
            + @fldName + "] from [" + @tblName + "] where (" + @strWhere + ") "
            + @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder

    if @PageIndex = 1
    begin
        set @strTmp = ""
        if @strWhere != ''
            set @strTmp = " where (" + @strWhere + ")"

        set @strSQL = "select top " + str(@PageSize) + " * from ["
            + @tblName + "]" + @strTmp + " " + @strOrder
    end

    if @IsCount != 0
        if @strWhere !=''
        set @strSQL = "select count(*) as Total from [" + @tblName + "]where("+@strWhere+")"
        else
        set @strSQL = "select count(*) as Total from [" + @tblName + "]"

    exec (@strSQL)
    GO





    ------------------------------
    Two:

    /*
     名称:spAll_DeleteNoneUnique
     输入:要查询的表名和字段列表
     输出:
     调用:
     说明:实现千万级数据的分页显示!可以在5秒内获取1448万条记录里的第1200页的100条记录,雄不?
     作者:铁拳
     邮件:
     网站:http://www.wellknow.net
     更新:20040610
     支持:http://bbs.wellknow.net
     版权:转述时请注明来源:用思维创造未来的Wellknow.net
    */

    CREATE PROCEDURE GetRecordFromPage
        @tblName      varchar(255),       -- 表名
        @fldName      varchar(255),       -- 字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
        @IsCount      bit = 0,            -- 返回记录总数, 非 0 值则返回
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(1000) = ''  -- 查询条件 (注意: 不要加 where)
    AS

    declare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(100)        -- 临时变量
    declare @strOrder varchar(400)        -- 排序类型

    if @OrderType != 0
    begin
        set @strTmp = "<(select min"
        set @strOrder = " order by [" + @fldName +"] desc"
    end
    else
    begin
        set @strTmp = ">(select max"
        set @strOrder = " order by [" + @fldName +"] asc"
    end

    set @strSQL = "select top " + str(@PageSize) + " * from ["
        + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
        + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
        + @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
        + @strOrder

    if @strWhere != ''
        set @strSQL = "select top " + str(@PageSize) + " * from ["
            + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
            + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
            + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
            + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

    if @PageIndex = 1
    begin
        set @strTmp = ""
        if @strWhere != ''
            set @strTmp = " where " + @strWhere

        set @strSQL = "select top " + str(@PageSize) + " * from ["
            + @tblName + "]" + @strTmp + " " + @strOrder
    end

    if @IsCount != 0
        set @strSQL = "select count(*) as Total from [" + @tblName + "]"

    exec (@strSQL)

    GO


    KINGMAP 发表于2004-07-30 1:36 PM  IP: 220.174.36.*
    修改一点点便于其他人使用
    CREATE PROCEDURE GetRecordFromPage
    @tblName varchar(255), -- 表名
    @fldName varchar(255), -- 字段名
    @OrderfldName varchar(255), -- 排序字段名
    @StatfldName varchar(255), -- 统计字段名
    @PageSize int = 10, -- 页尺寸
    @PageIndex int = 1, -- 页码
    @IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
    @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
    @strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
    AS

    declare @strSQL varchar(6000) -- 主语句
    declare @strTmp varchar(100) -- 临时变量
    declare @strOrder varchar(400) -- 排序类型

    if @OrderType != 0
    begin
    set @strTmp = "<(select min"
    set @strOrder = " order by [" + @OrderfldName +"] desc"
    end
    else
    begin
    set @strTmp = ">(select max"
    set @strOrder = " order by [" + @OrderfldName +"] asc"
    end

    set @strSQL = "select top " + str(@PageSize) + " " + @fldName + " from ["
    + @tblName + "] where [" + @OrderfldName + "]" + @strTmp + "(["
    + @OrderfldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
    + @OrderfldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
    + @strOrder

    if @strWhere != ''
    set @strSQL = "select top " + str(@PageSize) + " " + @fldName + " from ["
    + @tblName + "] where [" + @OrderfldName + "]" + @strTmp + "(["
    + @OrderfldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
    + @OrderfldName + "] from [" + @tblName + "] where " + @strWhere + " "
    + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

    if @PageIndex = 1
    begin
    set @strTmp = ""
    if @strWhere != ''
    set @strTmp = " where " + @strWhere

    set @strSQL = "select top " + str(@PageSize) + " " + @fldName + " from ["
    + @tblName + "]" + @strTmp + " " + @strOrder
    end

    if @IsCount != 0
    set @strSQL = "select count(" + @StatfldName + ") as Total from [" + @tblName + "]"

    exec (@strSQL)

    GO



  • 相关阅读:
    最近开始学习python,学习到了关于web的内容。
    redis requires Ruby version >= 2.2.2问题
    xml中CDATA包含问题
    pl/sql中文乱码问题解决
    关于window.open()中文传值乱码问题的解决方法
    关于maven中央仓库jar包不存在问题
    201571030332 扎西平措 《面向对象程序设计Java》第八周学习总结
    扎西平措 201571030332 《面向对象程序设计(java)课程学习进度条》
    扎西平措 201571030332 《面向对象程序设计 (JAVA)》第二周 学习总结
    扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结
  • 原文地址:https://www.cnblogs.com/xucanzhao/p/544574.html
Copyright © 2011-2022 走看看