1.返回记录集总数存储过程:
CREATE procedure dbo.recordCount
(
@TableName nvarchar(100),--数据库表名
@strWhere nvarchar(500),--查询条件
@count int output---输出值,记录集总数
)
as
declare @sqlStr nvarchar(1000)
if @strWhere != ''
set @sqlStr = N'select @COUNT = count(id) from '+ @TableName +' where 1=1 ' + @strWhere
else
set @sqlStr = N'select @COUNT = count(id) from '+ @TableName
exec sp_executesql @sqlStr,N'@count int output',@count output
GO
2.sql2000存储过程分页,这个也是在网上找的,有错误,自己修改过来了。
CREATE PROCEDURE dbo.sp_pageview
@tablename varchar(200) , --表名
@strGetFields varchar(200), --查询列名
@PageIndex int , --页码
@pageSize int, --页面大小
@strWhere varchar(100) , --查询条件
@strOrder varchar(100) , --排序列名
@intOrder bit --排序类型 1为升序
AS
begin
declare @strSql varchar(500) --主语句
declare @strTemp varchar(100) --临时变量
declare @strOrders varchar(50) --排序语句
declare @table varchar(70)
if @intOrder = 0
begin
--为0是升序
set @strTemp = '>(select max'
set @strOrders = ' order by '+@strOrder+' asc '
end
else
begin
--否则为降序
set @strTemp = '<(select min'
set @strOrders = ' order by '+@strOrder+' desc '
end
if @PageIndex =1 --第一页直接读出纪录
begin
if @strWhere = ''
begin
set @strSql = 'select top '+str(@pageSize)+' '+@strGetFields+' from '+@tablename+' '+@strOrders
end
else
begin
set @strSql = 'select top '+str(@pageSize)+' '+@strGetFields+ ' from '+@tablename+' where '+@strWhere+' '+@strOrders
end
end
else
begin
set @strSql = 'select top'+str(@pageSize)+' '+@strGetFields+' from '+@tablename+' where '+@strOrder+' '+@strTemp+' ('+@strOrder+')'
+' from (select top '+str((@pageIndex-1)*@pageSize)+' '+@strGetFields+' from '+@tablename+ ' '+@strOrders+ ') as tempTable ) '+@strOrders
if @strWhere != ' '
begin
set @strSql = 'select top '+str(@pageSize)+ ' '+@strGetFields+' from '+@tablename+ ' where '+@strOrder+ ' '+@strTemp+' ('+@strOrder+') '
+' from(select top '+str((@pageIndex-1)*@pageSize)+' '+@strGetFields+' from '+@tablename+' where '+@strWhere+' ' +@strOrders+') as tempTable) and '+@strWhere+' '+@strOrders
end
end
exec(@strSql)
end
GO
3.asp调用页面:list.asp
<!--#include file="conn.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script language="javascript">
function checkpage()
{
if(isNaN(document.fenye.page.value))
{
alert("跳转页码请输入数字!");
document.fenye.page.focus();
document.fenye.page.value='';
return false;
}
if(document.fenye.page.value=='')
{
alert("跳转页码不能为空!");
document.fenye.page.focus();
document.fenye.page.value='';
return false;
}
}
</script>
<title>存储分页</title>
<%
dim TableName,Page,TotalRs,PageNum,TotalPage,SearchChar,strGetFields,strOrder
'存储过程参数
TableName="cTongJi_product"
strGetFields=" id,ip,into_time "'字段开始结束加上空格
strOrder="id"
PageNum=30
'搜索关键字
key=request("key")
'按什么字段搜索
kind=request("kind")
'分页参数
page=cint(request("page"))
trs=request("trs")
tpa=request("tpa")
'查询条件判断
if key<>"" then
SearchChar=" and "& kind &" like '%"&key&"%'"
SearchChar1=" "& kind &" like ''%"&key&"%''"'书写注意与存储过程中的where关键字间的间距,如果多条件,后面也要有间距
else
SearchChar=""
SearchChar1=""
'response.Write SearchChar1
'response.End()
end if
'返回总记录数
Set MyComm=Server.CreateObject("ADODB.Command")
MyComm.ActiveConnection=conn
'MyConStr是数据库连接字串
MyComm.CommandText="recordCount"
'指定存储过程名
MyComm.CommandType=4
'表明这是一个存储过程
MyComm.Prepared=true
'要求将SQL命令先行编译
'声明参数
MyComm.Parameters.append MyComm.CreateParameter( "@TableName",200,1,500,tablename)
MyComm.Parameters.append MyComm.CreateParameter( "@strWhere",200,1,500,SearchChar)
MyComm.Parameters.append MyComm.CreateParameter( "@count",3,2)
MyComm.Execute
'取得出参
TotalRs=MyComm.Parameters( "@count").value
Set MyComm=Nothing
'计算总页数
if TotalRs mod 30 =0 then
TotalPage=TotalRs /30
else
TotalPage=TotalRs /30 + 1
end if
'页码合法判断
if page="" or page<=0 then
page=1
end if
'page=48
'调用存储过程
sql="exec sp_pageview '"&TableName&"','"&strGetFields&"',"&page&","&PageNum&",'"&SearchChar1&"','"&strOrder&"',0"
response.Write sql
'response.End()
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
%>
</head>
<body>
<table width="800" height="46" border="1" align="center">
<form action="list.asp" method="post">
<tr>
<td colspan="3">关键字:<input type="text" name="key" /> <input type="radio" value="how" name="kind" />功能<input type="radio" checked="checked" value="what" name="kind" />产品<input type="radio" value="ip" name="kind" />厂家
<input type="submit" name="Submit" value="搜 索" />
</td>
</tr></form>
</table>
<table width="800" height="46" border="1" align="center">
<tr>
<td height="35" align="center"><strong>代理区域</strong></td>
<td align="center"><strong>要求代理品种 </strong></td>
<td height="35" align="center"><strong>性质</strong></td>
<td height="35" align="center"><strong>发布时间</strong></td>
</tr>
<%
do while not rs.eof
%>
<tr>
<td height="30"><%=rs("id")%></td>
<td height="30"><%=rs("ip")%></td>
<td height="30"><%=rs("into_time")%></td>
<td height="30"> </td>
</tr>
<%
rs.movenext
loop
%>
</table>
<table width="800" height="46" border="1" align="center">
<form action="list.asp" method="post" name="fenye" onsubmit="return checkpage()">
<tr>
<td colspan="3" align="center">共<%=TotalRs%>条 <%=PageNum%>条/页 当前第<%=Page%>/<%=TotalPage%>页
<%if TotalPage=1 or TotalPage=0 then
response.Write " 首页 上一页 下一页 末页"
else
%>
<%if page=1 then
response.Write" 首页 上一页 <a href='list.asp?page="&page+1&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>下一页</a> <a href='list.asp?page="&TotalPage&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>末页</a>"
end if%>
<%if page>1 and page<TotalPage then
response.Write"<a href='list.asp?page=1&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>首页</a> <a href='list.asp?page="&page-1&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>上一页</a> <a href='list.asp?page="&page+1&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>下一页</a> <a href='list.asp?page="&TotalPage&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>末页</a>"
end if
end if
%>
<%if page=TotalPage then
response.Write"<a href='list.asp?page=1&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>首页</a> <a href='list.asp?page="&page-1&"&key="&key&"&kind="&kind&"&trs="&trs&"&tpa="&tpa&"'>上一页</a> 下一页 末页"
end if%>
转到第<input id="gopage" maxlength="10" size="5" value="<%=page%>" name="page"/>页<input id="submit123" type="submit" value="Go" name="submit123"/><input type="hidden" value="<%=TotalRs%>" name="trs"/><input type="hidden" value="<%=TotalPage%>" name="tpa"/>
<input type="hidden" name="keyword" value="<%=keyword%>"/><input type="hidden" name="kind" value="<%=kind%>"/></td>
</tr>
</form>
</table>
</body>
</html>
<%
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
作者:一天到晚游泳的鱼2016
来源:CSDN
原文:https://blog.csdn.net/sandyxxx/article/details/1922045