今天在網(wǎng)上看到了幾個(gè)解決sql2000的分頁查詢方法
寫在這里(沒有測(cè)試)
四種方法取表里n到m條紀(jì)錄:
1.
select top m * into 臨時(shí)表(或表變量) 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
如果你在執(zhí)行select identity(int) id0,* into #temp from tablename這條語句的時(shí)候報(bào)錯(cuò),那是因?yàn)槟愕腄B中間的select into/bulkcopy屬性沒有打開要先執(zhí)行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true
4.如果表里有identity屬性,那么簡(jiǎn)單:
select * from tablename where identitycol between n and m
**********************************************
*sql2000下 分頁存儲(chǔ)過程 **********************
**********************************************
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
--名稱:分頁存儲(chǔ)過程
--使用示例 EXEC sp_PageIndex '*',' FROM StuSources ',2,10
--注意
--目前還沒有對(duì)輸入的參數(shù)進(jìn)行嚴(yán)格的驗(yàn)證
--默認(rèn)為輸入都是合法有效的

ALTER PROC sp_PageIndex
@sqlSelect varchar(800) --SELECT 后面 FROM 前面 的 字段 不用包含SELECT
,@sqlFrom varchar(800) --FROM 后面 的 字段 包含F(xiàn)ROM
,@countPerPage int -- 每頁數(shù)據(jù)行數(shù)
,@toPage int --要轉(zhuǎn)到的頁碼

AS

BEGIN


-- 根據(jù)每頁數(shù)據(jù)行數(shù) 和 要轉(zhuǎn)到的頁碼 得到 數(shù)據(jù)起止點(diǎn)
Declare @start int
Declare @end int

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


-- 臨時(shí)表名稱 可隨機(jī)命名
Declare @tmpTable varchar(10)
SET @tmpTable ='#tmp'

Declare @sqlStr varchar(800)
-- 創(chuàng)建數(shù)據(jù)源到臨時(shí)表
SELECT @sqlStr = 'SELECT Identity(int,1,1) AS RowIndex,'
SELECT @sqlStr = @sqlStr + rtrim(@sqlSelect) + ' INTO '+ @tmpTable
SELECT @sqlStr = @sqlStr + rtrim(@sqlFrom)
-- 查詢臨時(shí)表 得到所需要的數(shù)據(jù)
SELECT @sqlStr = @sqlStr + ' '+'SELECT '+ rtrim(@sqlSelect) +' FROM ' + @tmpTable
SELECT @sqlStr = @sqlStr + ' WHERE RowIndex BETWEEN ' + Convert(char,@start) + " AND " + Convert(char,@end)
-- 刪除臨時(shí)表
SELECT @sqlStr = @sqlStr + ' '+'DROP TABLE '+@tmpTable
EXEC (@sqlStr)


END


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO