数据库存储过程分页显示

前端之家收集整理的这篇文章主要介绍了数据库存储过程分页显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注:需要建立索引
<div class="codetitle"><a style="CURSOR: pointer" data="25019" class="copybut" id="copybut25019" onclick="doCopy('code25019')"> 代码如下:

<div class="codebody" id="code25019">
/
经测试,在14483461条记录中查询第100000页,每页10条记录按升序和降序第一次时间均为0.47秒,第二次时间均为0.43秒,测试语法如下:
execGetRecordFromPagenews,newsid,10,100000
news为表名,newsid为关键字段,使用时请先对newsid建立索引。
/ /
函数名称:GetRecordFromPage
函数功能:获取指定页的数据
参数说明:@tblName包含数据的表名
@fldName关键字段名
@PageSize每页记录数
@PageIndex要获取页码
@OrderType排序类型,0-升序,1-降序
@strWhere查询条件(注意:不要加where)
作  者:铁拳
邮  箱:unjianhua_kki@sina.com">sunjianhua_kki@sina.com
创建时间:2004-07-04
修改时间:2004-07-04
/
CreatePROCEDUREGetRecordFromPage
@tblNamevarchar(255),--表名
@fldNamevarchar(255),--字段名
@PageSizeint=10,--页尺寸
@PageIndexint=1,--页码
@OrderTypebit=0,--设置排序类型,非0值则降序
@strWherevarchar(2000)=''--查询条件(注意:不要加where)
AS declare@strsqlvarchar(6000)--主语句
declare@strTmpvarchar(1000)--临时变量
declare@strOrdervarchar(500)--排序类型 if@OrderType!=0
begin
set@strTmp="<(selectmin"
set@strOrder="orderby["+@fldName+"]desc"
end
else
begin
set@strTmp=">(selectmax"
set@strOrder="orderby["+@fldName+"]asc"
end set@strsql="selecttop"+str(@PageSize)+"from["
+@tblName+"]where["+@fldName+"]"+@strTmp+"(["
+@fldName+"])from(selecttop"+str((@PageIndex-1)
@PageSize)+"["
+@fldName+"]from["+@tblName+"]"+@strOrder+")astblTmp)"
+@strOrder if@strWhere!=''
set@strsql="selecttop"+str(@PageSize)+"from["
+@tblName+"]where["+@fldName+"]"+@strTmp+"(["
+@fldName+"])from(selecttop"+str((@PageIndex-1)
@PageSize)+"["
+@fldName+"]from["+@tblName+"]where"+@strWhere+""
+@strOrder+")astblTmp)and"+@strWhere+""+@strOrder if@PageIndex=1
begin
set@strTmp=""
if@strWhere!=''
set@strTmp="where("+@strWhere+")" set@strsql="selecttop"+str(@PageSize)+"*from["
+@tblName+"]"+@strTmp+""+@strOrder
end exec(@strsql) GO

原文链接:https://www.f2er.com/mssql/64615.html

猜你在找的MsSQL相关文章