当前位置: 首页 > 图文教程 > 数据库 > MSSQL > 自己改写的一个sql server 2000的分页存储过程

MSSQL
开源MySQL公司停止提供企业版源代码tar包
细化解析:MySQL+Webmin轻松创建数据库
用mysql做站点时怎样记录未知错误的发生
SQL数据库操作类
如何利用SQL Server数据库快照形成报表
SQL Server中应当怎样得到自动编号字段
SQL Server数据库连接中常见的错误分析
详细讲解SQL Server数据库的文件恢复技术
轻松掌握SQL Server数据库的六个实用技巧
SQL Server数据库涉及到的数据仓库概念
深入了解SQL Server 2008 商业智能平台
剖析SQL Server 事务日志的收缩和截断
如何在不同版本的SQL Server中存储数据
怎样缩小SQL Server数据库的日志文件
SQL Server中两种修改对象所有者的方法
轻松掌握SQL Server存储过程的命名标准
怎样从旧版本SQL Server中重新存储数据
快速掌握如何使用SQL Server来过滤数据
教你快速掌握两个SQL Server的维护技巧
有效地使用 SQL事件探查器的提示和技巧

MSSQL 中的 自己改写的一个sql server 2000的分页存储过程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 117 ::
收藏到网摘: n/a

优点如下,简单,直接sql语句输入,高效,效率很高测试过了,不过最好是主键排序,还有少用视图 。

缺点也有,不支持复杂的sql语句,不能多字段排序,sql语句必须小于4000字符

/*
  经测试,在 14483461 条记录中查询第 100000 页,每页 10 条记录按升序和降序第一次时间均为 0.47 秒,第二次时间均为 0.43 秒,测试语法如下:
  exec GetRecordFromPage news,newsid,10,100000
  news 为 表名, newsid 为关键字段, 使用时请先对 newsid 建立索引。
*/

/*
  函数名称: GetRecordFromPage
  函数功能: 获取指定页的数据
  参数说明: @tblName      包含数据的表名
           @fldName      关键字段名
           @PageSize     每页记录数
           @PageIndex    要获取的页码
           @OrderType    排序类型, 0 - 升序, 1 - 降序
           @strWhere     查询条件 (注意: 不要加 where)
  作  者: 铁拳
  邮  箱: [email protected]
  创建时间: 2004-07-04
  修改时间: 2004-07-04
  http://blog.knowsky.com/
*/
CREATE   PROCEDURE GetRecordFromPage1
    @SQL     varchar(8000),      -- SQL语句
    @PageSize     int = 10,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    --@strOut       varchar(6000) output 输出处理完成的SQL语句
    @RecordCount  int = 0 output      -- 输出总记录数
AS

declare @strSQL   varchar(8000)       -- 最后获取的SQL语句
declare @strTmp   varchar(8000)       -- 临时变量
declare @strOrder varchar(500)        -- 排序类型
declare @tblName  varchar(255)        -- 表名
declare @fldName  varchar(255)        -- 字段名
declare @strWhere varchar(8000)       -- 查询条件 (注意: 不要加 where)
declare @tmpOrder varchar(255)        -- 排序字符串临时变量
declare @strFilds varchar(8000)       -- 需要显示的列
declare @intFilds int              -- 显示的列所在位置
declare @intOrder int                 -- 排序字符串位置
declare @intSQL   int              -- SQL语句长度
declare @intWhere int                 -- where字符串位置
declare @intTable int              -- 表名称位置
declare @strRsSql nvarchar(4000)      -- 统计总记录数sql语句

set @intOrder=CharIndex('order by',@SQL)
set @intSQL=Len(@SQL)

set @intFilds=CharIndex('select',@SQL)
set @strFilds=SubString(@SQL,@intFilds+7,@intSQL-@intFilds-1)
set @strFilds=SubString(@strFilds,1,CharIndex('from',@strFilds)-1)

set @tblName=SubString(@SQL,CharIndex('from',@SQL)+5,@intSQL-CharIndex('from',@SQL)+5)
set @intTable=CharIndex(' ',@tblName)
if @intTable>0
begin
    set @tblName=SubString(@tblName,1,@intTabl