今天在網上看到了幾個解決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