create procedure goalerpagesp
@intpagesize int,
@intcurrpage int,
@strfields nvarchar(2000),
@strtable varchar(200),
@strwhere varchar(800),
@strordertype varchar(200),
@strkeyfield varchar(50)
asset nocount on
declare @tmpsql nvarchar(4000)--存放動態sql語句
declare @tmpwhere varchar(800)
declare @tmpandwhere varchar(800)--用於第n(>1)頁上邊的查詢條件
declare @tmporder varchar(200)
declare @tmpd_x varchar(2)
declare @tmpmin_max varchar(3)
--設定條件--
if @strwhere is null or rtrim(@strwhere)=''
begin --沒有查詢條件
set @tmpwhere=''
&www.cppcns.comnbsp; set @tmpandwhere=''
end
else
begin --有查詢條件
set @tmpwhere=' where '+@strwhere
set @tmpandwhere=' and '+@strwhere
end
--設定排序--
if @strordertype != 0
begin--倒序
set @tmpd_x = ' set @tmpmin_max = 'min'
set @tmporder=' order by ' +@strkeyfield+ ' desc'
end
else
begin
set @tmpd_x = '>'
set @tmpmin_max = 'max'
set @tmporder=' order by ' +@strkeyfield+ ' asc'
end
--sql查詢--
if @intcurrpage=1
set @tmpsql='select top '+cast(@intpagesize as varchar)+' '+@strfields+' from '+@strtable+' '+@tmpwhere+' '+@tmporder
else
set @tmpsql='select top '+cast(@intpagesize as varchar)+' '+@strfields+' from '+@strtable+' where ('+@strkeyfield+' '+@tmpd_x+' (select '+@tmpmin_max+'('+@strkeyfield+') from (select top '+cast(@intpagesize*(@intcurrpage-1) as varchar)+' '+@strkeyfield+' from '+@strtable+' '+@tmpwhere+' '+@tmporder+') as t))'+@tmpandwhere+' '+@tmporder
exec(@tmpsql)
go呼叫方法:
intpagesize=20
strtable=" [tablename] " '資料表名稱
strfields=" field1,field2,field3,field4 " '需要讀取的列名
strkeyfi程式設計客棧eld="field1" '主鍵:這裡假設field1為主鍵
strwhere="" '條件:fielda='b'
strordertype=1 '排序方式:1為倒序,0為順序
currpage=request.querystring("page")
if(currpage<>"" and isnumeric(currpage))then
currpage=clng(currpage)
if(currpage<1)then currpage=1
else
currpage=1
end if
if strwhere<>"" then
tmpwhere=" where "&strwhere
else
tmpwhere=""
end if
if(session("reccount")<>"")then
if(session("strwhere")<>strwhere)then
reccount=conn.execute("select count("&strkeyfield&") from "&strtable&tmpwhere)(0)
session("reccount")=reccount
session("strwhere")=strwhere
else
reccount=session("reccount")
end if
else
reccount=conn.execute("select count(*) from "&strtable&tmpwhere)(0)
session("reccount")=reccount
session("strwhere")=strwhere
end if
if(reccount mod intpagesize <>0)then
intpagecount=int(reccount/intpagesize)+1
else
intpagecount=reccount/intpagesize
end if
set cmd=server.createobject("adodb.command")
cmd.commandtype=4
set cmd.activec
cmd.commandtext="goalerpagesp"
cmd.parameters.append cmd.createparameter("@intpagesize",4,1,4,intpagesize)
cwww.cppcns.commd.parameters.append cmd.createparameter("@intcurrpag程式設計客棧e",4,1,4,currpage)
cmd.parameters.append cmd.createparameter("@strfields",200,1,2000,strfields)
cmd.parameters.append cmd.createparameter("@strtable",200,1,200,strtable)
cmd.parameters.append cmd.createparameter("@strwhere",200,1,800,strwhere)
cmd.parameters.append cmd.createparameter("@strordertype",4,1,4,strordertype)
cmd.parameters.append cmd.createparameter("@strkeyfield",200,1,50,strkeyfield)
set rs=cmd.execute()
if reccount<1 then
response.write("沒有記錄")
else
getrecord=rs.getrows(intpagesize)
for i=0 to ubound(getrecord,2)
response.write(getrecord(0,i),getrecord(1,i),getrecord(2,i)) '...輸出內容
next
getrecord=null
end if
set rs=nothing
有用的朋友請自己慢慢除錯吧,總記錄是用asp來取的,儲存在session裡邊,如果每次都統計一次總記錄,將會非常費時,當然,如果你想在儲存過程裡來取總記錄和總頁數然後返回也是可以的,下邊是**:
--獲取記錄總數--
set @tmpsql='select @getrecordcounts=count('+@strkeyfield+') from '+@strtable+@tmpwhere
exec sp_executesql @tmpsql,n'@getrecordcounts int output',@getrecordcounts output
--獲取總頁數--
set @tempfolatnumber=@getrecordcounts%@intpagesize
if @getrecordcounts<=@intpagesize
set @getpagecounts=1
else
begin
if @tempfolatnumber != 0
set @getpagecounts=(@getrecordcounts/@intpagesize)+1
else
set @getpagecounts=(@getrecordcounts/@intpagesize)
end別忘了返回定義引數:
@getrecordcounts int output,--返回總記錄
@getpagecounts int output--返回總頁數
本文標題: 以前寫的乙個分頁儲存過程,剛才不小心翻出來的
本文位址:
乙個分頁儲存過程
page language c import namespace system.data import namespace system.data.sqlclient 訂單號 服務專案 預訂日期 操作人員 分配狀態 databinder.eval container.dataitem,offerid...
乙個分頁儲存過程
louje 分頁儲存過程 create procedure usp page pagesize int,頁大小 pageno int,頁號 tablename nvarchar 500 表名 keyword nvarchar 20 主鍵 fieldname nvarchar 2000 要顯示的欄位名...
乙個分頁儲存過程
create procedure sp page tb varchar 50 表名 col varchar 50 按該列來進行分頁 coltype int,col 列的型別,0 數字型別,1 字元型別,2 日期時間型別 orderby bit,排序,0 順序,1 倒序 collist varchar...