通用带分页的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-->
(你基本上不用修改很多东西,只需要把表名和字段名(以下描红的地方)改一下就行了,复杂的问题都在调用时的条件内容上)
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-->
相关文章推荐
- 通用SQL存储过程分页以及asp.net后台调用的方法
- ASP.NET长文章实现通用以及高效的分页方法
- Asp.net实现通用以及高效的分页方法
- SqlServer通用分页的调用方法
- Asp.net实现通用以及高效的分页方法
- 通用SQL存储过程分页以及asp.net后台调用
- ASP调用.net的webservices的实现方法
- ASP.net中的几种分页方法
- asp.net中的datalist分页方法
- 一个SQL存储过程分页和ASP调用
- ASP+FSO+框架实现ASP生成htm并分页的方法(批量)
- Asp.net中SQL/Oracle通用分页控件(摘抄)
- [我的ASP.net学习历程]调用类库函库的简单加密方法
- 在asp中调用存储过程的几种方法
- ASP通用分页函数
- asp + oracle 分页方法 (不用存储过程)
- ASP.NET结合存储过程写的通用搜索分页程序
- 使用API调用Winodws各种通用对话框(Common Diaglog)的方法
- ASP学习:史上最强的数据分页方法