• 通用分页存储过程


    CREATE PROCEDURE pagelist

    @tblName varchar(255)="", -- 表名

    @strGetFields varchar(1000) = "*", -- 需要返回的列

    @fldName varchar(255)="id", -- 排序的字段名

    @PageSize int =10, -- 页尺寸

    @PageIndex int = 1, -- 页码

    @doCount bit = 0, -- 返回记录总数, 非 0 值则返回

    @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序

    @strWhere varchar(1500) = "1=1"-- 查询条件 (注意: 不要加 where)

    AS

    declare @strSQL varchar(5000) -- 主语句

    declare @strTmp varchar(110) -- 临时变量

    declare @strOrder varchar(400) -- 排序类型

    if @doCount != 0

    begin

    if @strWhere !=""

    set @strSQL = "select count(*) as Total from [" + @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代码 秦皇天下 qhtx.net 编辑

    set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " 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) +" "+@strGetFields+ " from ["

    + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["

    + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["

    + @fldName + "] from [" + @tblName + "] where " + @strWhere + " "

    + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder

    end

    end

    --print @strSQL

    exec (@strSQL)

    Technorati : sql 分页

  • 相关阅读:
    从统计学statistics的观点看概率分布
    JavaScript中getBoundingClientRect()方法详解
    insertRule()与addRule()创建规则
    Canvas 数学、物理、动画学习笔记一
    【超级干货】手机移动端WEB资源整合:转载
    JavaScript性能优化技巧之函数节流
    @font-face的用法
    Modernizr——为HTML5和CSS3而生!
    url、href、src 详解
    认识cookie与session的区别与应用
  • 原文地址:https://www.cnblogs.com/oneroom/p/1652632.html
Copyright © 2020-2023  润新知