sql2000 分页查询

sql2000 分页查询

今天在网上看到了几个解决sql2000的分页查询方法
写在这里(没有测试)

四种方法取表里n到m条纪录:

1.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量 order by columnname desc

2.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc

3.如果tablename里没有其他identity列,那么:
select identity(int) id0,* into #temp from tablename

取n到m条的语句为:
select * from #temp where id0 >=n and id0 <= m

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true

4.如果表里有identity属性,那么简单:
select * from tablename where identitycol between n and m
**********************************************
 *sql2000下 分页存储过程 **********************

**********************************************

SET
 QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO
-- 名称:分页存储过程
--
使用示例 EXEC sp_PageIndex '*',' FROM StuSources ',2,10
--
注意 
--
目前还没有对输入的参数进行严格的验证
--
默认为输入都是合法有效的

ALTER    PROC  sp_PageIndex
 
@sqlSelect   varchar ( 800 -- SELECT 后面 FROM 前面 的 字段 不用包含SELECT
, @sqlFrom   varchar ( 800 -- FROM 后面 的 字段 包含FROM
, @countPerPage   int   --  每页数据行数
, @toPage   int   -- 要转到的页码

AS

BEGIN


--  根据每页数据行数 和 要转到的页码 得到 数据起止点
Declare   @start   int
Declare   @end   int

set   @end   =   @countPerPage   *   @toPage
set   @start   =   @countPerPage   *  ( @toPage   -   1 +   1


--  临时表名称 可随机命名
Declare   @tmpTable   varchar ( 10 )
SET   @tmpTable   = ' #tmp '

Declare   @sqlStr   varchar ( 800 )
--  创建数据源到临时表
SELECT   @sqlStr   =   ' SELECT Identity(int,1,1) AS RowIndex, '
SELECT   @sqlStr   =   @sqlStr   +   rtrim ( @sqlSelect +   '  INTO   ' +   @tmpTable  
SELECT   @sqlStr   =   @sqlStr   +   rtrim ( @sqlFrom
--  查询临时表 得到所需要的数据
SELECT   @sqlStr   =   @sqlStr   +   '   ' + ' SELECT  ' +   rtrim ( @sqlSelect + '  FROM  '   +   @tmpTable  
SELECT   @sqlStr   =   @sqlStr   +   '  WHERE  RowIndex BETWEEN  '   +   Convert ( char , @start +  "  AND  "  +   Convert ( char , @end )
--  删除临时表
SELECT   @sqlStr   =   @sqlStr   +   '   ' + ' DROP TABLE  ' + @tmpTable
EXEC  ( @sqlStr )


END


GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

你可能感兴趣的:(sql2000 分页查询)