zoukankan      html  css  js  c++  java
  • 一个通用的分页存储过程(原创)

    2006年6月14日  作者:meil  博客:meil.livebaby.cn

    本文版权归作者meil转载请注明出处 

            这是我项目中使用的一个分页存储过程,具有很强的通用性。配合前台ASP.NET使用50万条数据基本感不到延迟。数据库为SQLServer2000。

     

    1. 分页存储过程(源码)

    CREATE   procedure pagination

     @str_sql           varchar(1000) = '*',     -- 执行的SQL 不含Order by 内容  
     @str_orderfield    varchar(255)='''',       -- 排序的字段名 
     @page_size         int = 10,                     -- 页大小 
     @page_index        int = 0,                      -- 页码
     @order_type        int,                           -- 设置排序类型, 非 -1 值则降序 
     @total_count       int   output                 -- 返回记录总数, 非 0 值则返回 
    as

    ---------------------
    -- 获取指定页的数据--
    ---------------------


    declare @strsql   varchar(5000)              -- 主语句
    declare @strtmp   varchar(5000)             -- 临时变量
    declare @strorder varchar(400)              -- 排序字串
    declare @cruRow   int                            -- 当前行号
     

    --执行总数统计
    exec getRowCount @str_sql,@total_count output

    set @strtmp =  ' select * from ' +
            '      (select top ' + convert(varchar(10),@page_size) + ' * from ' +
            '         (select top ' + convert(varchar(10),(@page_index + 1) * @page_size)  +' * from '        -- N+1页

    --排序方向
    if @order_type !=0
     begin
     set @strsql= @strtmp +
           '          order by @str_orderfield asc) a ' +
           '       order by @str_orderfield desc)b' +
                  ' order by @str_orderfield asc'
     end
    else
     begin
     set @strsql= @strtmp +
           '          order by @str_orderfield desc) a ' +
           '       order by  @str_orderfieldasc)b' +
                  ' order by  @str_orderfield desc'
     end

    exec (@strsql)

    GO

    ----------------------------------------------------------------------------

     2. 分页存储过程(源码) 执行中用到的行数统计

    create  procedure getRowCount
           @sql    nvarchar(2000),
           @count  int output
    as
    begin

    --------------------
    -- 获取数据总行数 --
    --------------------

      declare @tmpsql nvarchar(2000)
      set @tmpsql='select @count=count(*)  from ('+ @sql +') a'

      execute sp_executesql @tmpsql,N'@count int output',@count output
     
    end

    GO

    我来自:向东博客
  • 相关阅读:
    再学 GDI+[47]: 路径 CloseFigure
    再学 GDI+[46]: 路径 Create、FillPath、DrawPath
    学习官方示例 TApplication.ExeName
    再学 GDI+[45]: 文本输出 在矩形中格式化输出
    再学 GDI+[50]: 路径 GetPathPoints、GetPathTypes、TPathData、GetPathData
    再学 GDI+[49]: 路径 GetPointCount、GetPathPoints、GetLastPoint、GetBounds
    再学 GDI+[48]: 路径 StartFigure、CloseFigure、CloseAllFigures
    微软DevWow博客达人征文大赛获奖名单
    开始组建博客园北京俱乐部
    .NET技术大会
  • 原文地址:https://www.cnblogs.com/meil/p/435670.html
Copyright © 2011-2022 走看看