create proc myx_prpagerecordset
@querystr nvarchar(1000),
@keyfield nvarchar (200),
@pagesize int,
@pagenumber int
as begin
declare @sqltext as nvarchar(4000)
declare @sqltable as nvarch
set @sqltable = 'select top ' + cast((@pagenumber + 1) * @pagesize as varchar(30)) + '&nbthninpasp;'&nbswww.cppcns.comp;+ @querystr
set @sqltext =
'select top ' + cast(@pagesize as varchar(30)) + ' * ' +
'from (' + @sqltable + ') as tablea ' +
'where ' + @keyfield + ' not in(select top ' +
cast(@pagenumber * @pagesize as varchar(30)) + ' ' + @keyfield +
' from (' + @sqltable + ') as tableb)'
exec (@sqltext)
end
go 核心**
dim strsql as string
mycomm = new sqlclient.sqlcommand("myx_prpagerecordset", myconn)
mycomm.commandtype = commandtype.storedprocedure
mycomm.parameters.add(new sqlclient.sqlparameter("@querystr", sqldbtype.nvarchar, 1000))
mycomm.parameters("@querystr").value = " * from tbpage order by id desc"
mycomm.parameters.add(new sqlclient.sqlparameter("@keyfield", sqldbtype.nvarchar, 200))
mycomm.parameters("@keyfield").value = "[id]"
mycomm.parameters.add(new sqlclient.sqlparameter("@pagesize", sqldbtype.nvarchar, 1000))
mycomm.parameters("@pagesize").value = p程式設計客棧agesize
mycomm.parameters.add(new sqlclient.sqlparameter("@pagenumber", sqldbtype.nvarchar, 1000))
mycomm.parameters("@pagenumber").value = mypage - 1
呵呵,執行幾w條的**只需150毫秒左右
建立乙個test(id,name,fid)
向test添充幾十條資料,使id=1,2,3,4.........(即遞增的integer),其他任意
在t-sql debugger給改儲存過程分別傳遞如下引數:
@querystr= * from test
@keyfield=[id]
@pagesize=3
@pagenumber=1
問題出來了,看輸出結果(注意id):
id name fid
4 kwklover 2
5 kwklover 2
6 kwklover 2
根據傳入引數,我們的預期應該是:
id name fid
1 kwklover 2
2 kwklover 2
3 kwklover 2
下面是我參照小春的儲存分頁寫的分頁儲存過程,可以解決上面的問題:
create procedure prgetrecordbypage
( @pagesize int, --每頁的記錄條數
@pagenumber int, --當前頁面
@querysql varchar(1000),--部分查詢字串,如* from test order by id desc
@keyfield varchar(500)
) as
begin
declare @sqltable as varchar(1000)
declare @sqltext as varchar(1000)
set @sqltable='select top '+cast(@pagenumber*@pagesize as varchar(30))+' '+@querysql
set @sqltext='select top '+cast(@pagesize as varchar(30))+' * from '
+'('+@sqltable+') as tembtba '
+'where '+@keyfield+' not in (select top '+cast((@pagenumber-1)*@pagesize as varchar(30))+' '+@keyfield+' from '
+'('+@sqltable+') as temptbb)'
exec(@sqltext)
end
go
本文標題: sql 儲存過程分頁
本文位址: /wangluo/asp/19774.html
sql 儲存過程分頁
create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...
SQL 儲存過程 分頁
1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...
SQL 分頁儲存過程
create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...