sql server 存储过程分页

sql server 存储过程分页 - 应用软件 - 电脑教程网

sql server 存储过程分页

日期:2007-10-03   荐:
·列出 SQL Server 数据库中所有的存储过·SQL Server SQL语句导入导出大全·SQL Server SQL语句调优技巧·SQL Server 事务日志·从SQL SERVER 向ORACLE 8迁移的技术实·重建索引提高SQL Server性能·[收藏]利用SQL Server Reporting Servi·SQL Server 中死锁产生的原因及解决办·SQL Server 存储过程的分页方案比拼·Asp.Net(C#) Sql Server三层架构下数据 CREATE PROCEDURE [dbo].[Select_page_data_allow_fields_cond] @allow_fields varchar(100) =" * ", @DefRecordNum int =20, @PageNo int =1, @Where varchar(500) = null AS declare @R_end int declare @S_sql varchar(500) declare @condition varchar(500) declare @condition2 varchar(500) --if rtrim(@Where)!="" --if rtrim(@Where)!="" SET @r_end=(@PageNo-1)*@DefRecordNum 1 --正则页码 IF ( CAST(@r_end AS INT)<1) SET @r_end = 1 SET @Where=rtrim(@Where) --if not (@Where is null ) if (@Where <>'' ) SET @S_sql="SELECT TOP " CAST(@DefRecordNum AS CHAR(4)) space(2) @allow_fields " FROM BBSReply WHERE id>=(SELECT MAX(ID) AS r_begin FROM (SELECT TOP " CAST(@R_end AS CHAR(5)) " ID FROM BBSReply WHERE " @Where " ORDER BY ID) airzen) AND " @Where " order by id" ELSE SET @S_sql="SELECT TOP " CAST(@DefRecordNum AS CHAR(4)) space(2) @allow_fields " FROM BBSReply WHERE id>=(SELECT MAX(ID) AS r_begin FROM (SELECT TOP " CAST(@R_end AS CHAR(5)) " ID FROM BBSReply ORDER BY ID) airzen) order by id" --PRINT @S_sql EXEC(@S_sql) GO --------------------------------------------------- <% '******************************************************************** '本程序由AIRZEN 2004/5/12 更新修定。 '=================================================================== Option Explicit 'Response.Flush Dim BeginTime,EndTime BeginTime=Timer Dim conn,SQLstr,Rs,DefRecordNum,CursorBegin,CursorEnd,CurPageNum,hav DefRecordNum=20 dim R_end '--------------获取相关参数---------- If Request("CurPageNum")<>"" Then CurPageNum=CLng(Request("CurPageNum")) If CurPageNum<=0 Then CurPageNum=1 Else CurPageNum=1 End If '----------------End----------------- '------------显示翻页内容函数-------- Function TurnPageFS(DispRecordNum) Dim n While Not(Rs.Eof) And n<DispRecordNum n=n 1 Response.Write "<tr>"&_ "<td bgcolor='efefef'>"&n&"</td>"&_ "<td bgcolor='efefef'>"&Rs(0)&"</td>"&_ "<td bgcolor='efefef'>"&Rs(1)&"</td>"&_ "<td bgcolor='efefef'>"&Rs(2)&"</td>"&_ "<td bgcolor='efefef'>"&Rs(3)&"</td>"&_ "<td bgcolor='efefef'>"&Rs(4)&"</td>"&_ "<td bgcolor='efefef'>"&Rs(5)&"</td>"&_ "</tr>" If n=1 Then CursorBegin=Rs(0) If n=DefRecordNum Or Rs.Eof Then CursorEnd=Rs(0) Rs.MoveNext Wend End Function '-------------连接数据库------------- Set conn=Server.CreateObject("Adodb.Connection") 'SQLstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.Mappath("mldata.mdb") SQLstr="provider=SQLOLEDB;server=(local);database=XX;uid=XX;pwd=XX;" conn.Open SQLstr Dim TotalRecords,TotalPages TotalPages=clng(request.QueryString("TotalPages")) '//判断有无接收的页码 if TotalPages=0 or not isnumeric(TotalPages) then '//无接收页码 SQLstr="Select count(ID) As RecordSum From BBSreply WHERE userid='airzen'" '//此条件应与下面的存储过程条件保持一持 Set Rs=conn.Execute(SQLstr,0,1) TotalRecords=Rs("RecordSum") TotalPages=Abs(Int(TotalRecords/DefRecordNum*(-1))) Rs.Close Set Rs=Nothing end if dim allow_fields '//允许控制选择的字段以最优化的SQL 语句执行, '//allow_fields :允许的字段 '//DefRecordNum :每页显示的记录数目 '//CurPageNum :当前页码 allow_fields="[*]" if TotalPages<1 then TotalPages=1 SQLstr="Select_page_data_allow_fields_cond "&allow_fields&","&DefRecordNum&","&CurPageNum&",[userid='airzen']" '//此处的条件应与上面的保持一致 '//SQLstr="Select_page_data_allow_fields_cond "&allow_fields&","&DefRecordNum&","&CurPageNum&",[ id=1449236]" response.write "<br>"&SQLstr Set Rs=conn.Execute(SQLstr) %> <HTML> <head> <title>(airzen 2004/05/13 Version of Procedure)</title> <meta http-equiv="Content-Type" content="text/HTML; charset=gb2312"> <style type="text/CSS">td,br,div,p,body {font-size:12px}</style> </head> <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0"> <table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#E2F5FE"> <tr align="center"> <td colspan="2"><%Response.Write CurPageNum&"/"&TotalPages&"页 总记录数:"&TotalRecords%></td> <td>首页</a> <a href=../../"?CurPageNum=<%=CurPageNum-1%>"></a></a> </td> </tr> </table> <table width="100%" border="1" cellspacing="0" cellpadding="3" bgcolor="#CCCCCC"> <tr> <td>ID</td> <td>Title</td> <td>FileName</td> <td>大小</td> <td>尺寸</td> <td>类别</td> </tr> <% TurnPageFS(DefRecordNum) Rs.Close Set Rs=Nothing conn.Close Set conn=Nothing %> </table> <table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#E2F5FE"> <tr align="center"> <td colspan="2"><%Response.Write CurPageNum&"/"&TotalPages&"页 总记录数:"&TotalRecords%></td> <td><a href=../../"?TotalPages=<%=TotalPages%>">首页</a></a> <a href=../../"?TotalPages=<%=TotalPages%>&CurPageNum=<%=CurPageNum-1%>"></a></a> <a href=../../"?TotalPages=<%=TotalPages%>&CurPageNum=<%=TotalPages%>">末页</a></td> </tr> </table> <p class="unnamed1"> <% EndTime=Timer Response.Write "<br>程序执行时间:"&(EndTime-BeginTime)*1000&"毫秒" Response.Write " 第一条记录的ID值(CursorBegin)="&CursorBegin&" " Response.Write "最后一条记录的ID值(CursorEnd)="&CursorEnd&"<br><br>" %> </p> <p>&nbsp;</p> </body> </HTML> 本程序思路用到存储过程, ASP页面中加入传递参数TotalPages 第一页运行时将会多做一次获取总页数的运算,在以后的页面中将会用参数传递。这样将节省一些时间。
标签: