实现千万级数据分页的存储过程!《

来源:百度文库 编辑:神马文学网 时间:2024/04/29 17:01:55
续》

                    铁 拳


/*
  此存储过程的思路是将关键字段与无重复索引字段结合起来进行排序,如果关键字
  段有重复,可再按索引字段进行排序从而进一步分页,需强调的是 @fldIndex 字
  段类型必须为可参与数学计算的数据类型,即使 @fldIndex 字段有重复也可使
  用,只要 @flName 字段和 @fldIndex 不同时具有重复数据即可正常使用。例
  如下表所示:

 IDCustIDBirthday
131982-03-10
231979-10-04
341980-09-02
  如果按 ID 排序,则可以使用 ID 作为 @fldName 并且省略 @fldIndex 参数,
  如果按 CustID 排序则将 CustID 作为 @fldName 并以 ID 作为 @fldIndex
  字段,如果同时出现 ID 与 CustID 重复的情况下就不适合使用这种参数,例如:

 
IDCustIDBirthday
131982-03-10
131979-10-04
341980-09-02
  只要 @fldIndex 和 @fldName 字段不同时出现重复就可以了,如果数据中具有
  唯一索引的主键字段,则应使用该字段作为 @fldIndex 参数,无需其它条件。

  SQL Server 查询分析器中调用方法如下:
  exec GetRecordFromPage ‘TableName‘, ‘SortField‘, ‘IndexField‘, 10, 5, 0,
                         ‘IndexField > 10 AND IndexField <> 100‘

  更多参考请见:
    http://blog.csdn.net/sun_jianhua/archive/2004/07/04/33574.aspx

*/

/*
  函数名称: GetRecordFromPage
  函数功能: 获取指定页的数据
  参数说明: @tblName      包含数据的表名
           @fldName      关键字段名
           @fldIndex     无重复索引字段
           @PageSize     每页记录数
           @PageIndex    要获取的页码
           @OrderType    排序类型, 0 - 升序, 1 - 降序
           @strWhere     查询条件 (注意: 不要加 where)
  作  者: 铁拳
  邮  箱: sunjianhua_kki@sina.com
  创建时间: 2004-07-04
  修改时间: 2005-09-21
*/
CREATE PROCEDURE GetRecordFromPage
    @tblName      varchar(255),       -- 表名
    @fldName      varchar(255),       -- 字段名
    @fldIndex     varchar(255)=‘‘,    -- 无重复索引字段
    @PageSize     int = 10,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
    @strWhere     varchar(2000) = ‘‘  -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL   varchar(6000)       -- 主语句
declare @strTmp   varchar(1000)       -- 临时变量
declare @strOrder varchar(500)        -- 排序类型
declare @strField varchar(1000)       -- 用来联合的字段

if @PageIndex < 1
    return

set @strField = ‘[‘ + @fldName + ‘]‘

-- 计算后的数据精确到小数点后 33 位
if @fldIndex != ‘‘
    set @strField = ‘ convert(numeric(38,33), [‘ + @fldName
        + ‘]) + convert(numeric(38,33), [‘ + @fldIndex
        + ‘] / 10000000000000000000000000000) ‘

if @OrderType != 0
begin
    set @strTmp = ‘<(select min‘
    set @strOrder = ‘ order by ‘ + @strField + ‘ desc‘
end
else
begin
    set @strTmp = ‘>(select max‘
    set @strOrder = ‘ order by ‘ + @strField + ‘ asc‘
end

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

if @strWhere != ‘‘
    set @strSQL = ‘select top ‘ + str(@PageSize) + ‘ * from [‘
        + @tblName + ‘] where ‘ + @strField + @strTmp
        + ‘(tmpIndex) from (select top ‘ + str((@PageIndex-1)*@PageSize) + @strField
        + ‘ tmpIndex 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

exec (@strSQL)
GO