/*****听以前的同事说asp页面上的分页太慢了(如果数据多了),
就想了这么个笨办法。有些地方还要考虑—-比如select top 22 * from cat_list
where t_id not in (select t_id from #change)是否有效率问题;数据不能重复等等
不过灵活性挺好。希望各位高手再给帮忙改正;多谢chair3的帮助—这个存储过程还可以在加入几个变量,随便大家改吧:)*****/
create proc page
@pagenum int
as
set nocount on /*—–这一句很重要哦:)),不然它只会认 insert #change……这个数据集:))*/
declare @sql nvarchar(500) –声明动态sql执行语句
declare @pagecount int –当前页数
–取得当前数据库的记录总数
declare @row_num int
begin
select @row_num=count(*) from cat_list
–创建临时表,作为数据过滤
create table #change (t_id int)
–判断当前页数
if @row_num>6 –大于页面显示记录数,则分页
begin
set @row_num=@pagenum*6
if @row_num=6
select top 6 * from cat_list
else
begin
set @row_num=(@pagenum-1)*6
set @pagecount=@row_num
set @sql=ninsert #change (t_id) select top +cast(@pagecount as char(100))+ t_id from cat_list where t_id not in (select t_id from #change)
exec sp_executesql @sql
select top 6 * from cat_list where t_id not in (select t_id from #change)
end
end
else –只现实全部的数据
select * from cat_list
end
go
客户端这样调用:
<!–#include file="conn.asp"–>
<!–#include file="adovbs.inc"–>
<% dim t_com
dim t_rsdeclare datamanage recordset
dim parameters
set t_com=server.createobject("adodb.command")
t_com.activeconnection=conn
t_com.commandtext="page"
t_com.commandtype=adcmdstoredproc
t_com.prepared=true
set parameters=t_com.createparameter("@pagenum",adinteger,adparaminput)
t_com.parameters.append parameters
dim page
page=request.querystring("page")
if page="" then
page=1
end if
t_com("@pagenum")=page
set t_rs=t_com.execute
do while not t_rs.eof
response.write t_rs("c_name")
t_rs.movenext
loop
%>
<a href="a.asp?page=<%=page+1%>">ddddd</a>
