您的位置:首页 > 编程语言 > ASP

通用带分页的sql2000存储过程和asp调用方法

2017-04-10 21:09 267 查看
通过百度搜索带颁的存储过程,虽然有很多,但都是问题很多,而且没有写明如何带条件的查询等,写个文章太不负责任了,经过长时间的研究,本人打造了自己的带分页存储过程,里面可以定义表名,列名,当然你也可以将表名和列名使用传递的方式,我觉得没什么必要,所以没有用传递,只是在内部进行设定,以下源码:

(你基本上不用修改很多东西,只需要把表名和字段名(以下描红的地方)改一下就行了,复杂的问题都在调用时的条件内容上)

create procedure test

(

/* pagethis是当前页码,pagesize是每页多少条记录,tj是反馈的条件,allsl是返回记录总数 */

@pagethis int,@pagesize int,@tj nvarchar(300),

@allsl int=0 output

)

as

begin

declare @sql nvarchar(500);

declare @alltj1 nvarchar(300);--结合where使用

declare @alltj2 nvarchar(300);--结合and使用

declare @tabname nvarchar(30);--表名称

declare @tablie nvarchar(300);--要选择字段名(列名)

/*初始化条件*/
set @tabname=' news ';

set @tablie=' id,title ';

set @alltj1='';set @alltj2='';

if @tj<>''

begin

set @alltj1=' where '+@tj;set @alltj2=' and '+@tj;

end

if (@pagethis=0 and @pagesize=0)

/*统计记录总数*/

begin

set nocount on;

set @sql='select count(id) as allsl from '+@tabname+@alltj1;

exec(@sql);

set nocount off;

end

else

/*取数据记录*/

begin

set nocount on;

if @pagethis=1

begin

set @sql='select top '+str(@pagesize)+@tablie+' from '+@tabname+@alltj1+' order by id desc';

end

else

begin

set @sql='select top '+str(@pagesize)+@tablie+' from '+@tabname+' where (id<(select min(id) from (select top '+str(@pagesize*(@pagethis-1))+' id from '+@tabname+@alltj1+' order by id desc) as temptable)) '+@alltj2+' order by id desc';

end

execute(@sql);

set nocount off;

end

end

go

以下是asp调用方法:

<%

'如果条件为空,那么gctj="''",注意,这里的内容是双引号里面有两个单引号,条件的前面不用带where

'看里面的条件,最左最右是有一个单引号包含的,如果中间的内容有单
8d70
引号,可用两个连续的单引号来替换

gctj=" ' (title like ''%中%'' or convert(nvarchar(300),tjly) like ''%中%'')' "

gcname=" test "

'取总数

set test=conn.execute("exec "&gcname&" 0,0,"&gctj)

allsl=test("allsl"):set test=nothing

'分页设定

totalrec=allsl:ipagesize=20:currentpage=getint(get1("page"))

n=totalrec\ipagesize:if totalrec mod ipagesize<>0 then n=n+1

if currentpage>n then currentpage=n

if currentpage<1 then currentpage=1

'用存储过程调用数据

rs.open "exec "&gcname¤tpage&","&ipagesize&","&gctj,conn,1,1

do while not rs.eof

%>

<div><%=rs("name")%></a></div>

<%rs.movenext:loop:rs.close%>

<!--pagelist-->

这里是分页代码,用你的分页代码配合使用即可

<!--pagelist-->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: