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

MSSQL
用SQL语句实现替换字符串
mssql查找备注(text,ntext)类型字段为空的方法
MSSQL数据类型及长度限制详细说明
SQL Server下几个危险的扩展存储过程
一些SQL Server存储过程参数及例子
sql高级技巧几个有用的Sql语句
SQL语句实现SQL Server 2000及Sql Server 2005日志收缩(批量)
用SQL建立索引的方法步骤
MSsql每天自动备份数据库并每天自动清除log的脚本
mssql无数据库日志文件恢复数据库的方法
SQL Server常用管理命令小结
SQL SERVER性能优化综述(很好的总结,不要错过哦)
sqlserver 游标的简单示例
sqlserver只有MDF文件恢复数据库的方法
在SQL Server启动时自动执行存储过程。
在 SQLSERVER 中快速有条件删除海量数据
阿拉伯数字转大写中文_财务常用sql存储过程
SQL Server存储过程的基础说明
列出SQL Server中具有默认值的所有字段的语句
文本、Excel、Access数据导入SQL Server2000的方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 90 ::
收藏到网摘: 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