您好,欢迎访问一九零五行业门户网

sqlserver 通用存储过程分页代码

分页存储过程大致有下列几种
1、 利用not in 和select top
2、 利用id大于多少和select top
3、 利用sql中的游标
4、临时表
可以参看网上的以下链接
c#中常用的分页存储过程小结
http://read.newbooks.com.cn/info/174545.html
在2005中我们的选择就多了,可以利用新语法cte(公用表表达式),关于cte的介绍大家可以参看博客园中一位仁兄的系列教程
http://www.cnblogs.com/nokiaguy/archive/2009/01/31/1381562.html
或者干脆上微软的官网
http://msdn.microsoft.com/zh-cn/library/ms190766(sql.90).x
查看具体内容。
除此之外还可以利用在2005中新增的一些函数,分别是:row_number(),rank,dense_rank,ntile,这些新函数是您可以有效的分析数据以及向查询饿结果行提供排序值。您可能发现这些新函数有用的典型方案包括:将连续整数分配给结果行,以便进行表示、分页、计分和绘制直方图。
详细介绍参见下列链接
http://blog.csdn.net/htl258/archive/2009/03/20/4006717.aspx
我这里主要使用的就是row_number()结合新语法cte,先贴上我的存储过程。设计,开发,测试存储过程和相关的c#代码就花费我两天的时间,不过后面的相似界面就很快了,一上午就可以搞两个分页显示的页面,就算是复杂的查询,一上午也可以搞定。
下面的存储过程没有将总页数和总条目数返回,如果你有兴趣,可以自己加上,可以参看 c#中常用的分页存储过程小结中的下列部分
declare @sql nvarchar(4000);
declare @totalrecord int;
--计算总记录数
if (@sqlwhere ='''' or @sqlwhere='' or @sqlwhere is null)
set @sql = 'select @totalrecord = count(*) from ' + @tablename
else
set @sql = 'select @totalrecord = count(*) from ' + @tablename + ' where ' + @sqlwhere
exec sp_executesql @sql,n'@totalrecord int output',@totalrecord output--计算总记录数
--计算总页数
select @totalpage=@totalrecord --ceiling((@totalrecord+0.0)/@pagesize)
存储过程sql如下,支持不定列,不定条件,多表联合,排序任意
复制代码 代码如下:
set ansi_nulls on
set quoted_identifier on
go
--declare @sql nvarchar(4000);
--declare @totalrecord int;
----计算总记录数
--if (@sqlwhere ='''' or @sqlwhere='' or @sqlwhere is null)
--set @sql = 'select @totalrecord = count(*) from ' + @tablename
--else
--set @sql = 'select @totalrecord = count(*) from ' + @tablename + ' where ' + @sqlwhere
--exec sp_executesql @sql,n'@totalrecord int output',@totalrecord output--计算总记录数
--
----计算总页数
--
--select @totalpage=@totalrecord --ceiling((@totalrecord+0.0)/@pagesize)
-- =============================================
-- author: shiwenbin
-- msn:jorden008@hotmail.com
-- email: jorden008@163.com
-- create date: 2009-10-20
-- description: 分页存储过程,根据传递的参数返回分页的结果
-- parameters:
-- =============================================
alter procedure [dbo].[proc_getdatapaged]
-- add the parameters for the stored procedure here
@strselect varchar(max)=null, --欲显示的列(多列用逗号分开),例如:id,name
@strfrom varchar(max)= null, --表名称,或者是表连接字符串,多表连接例如:student as s inner join dwinfo as dw on s.dwbh=dw.bh
@strwhere varchar(max)=null, --查询条件,''代表没有条件,单条件或者多条件,多条件例如:name='啊' and id=10
@strorder varchar(max) =null, --排序列(多个排序列用逗号分开),例如:id desc,name as
--@pagecount int output, --总页数
@itemcount bigint output, --总记录数
@pagesize int =50, --每页显示条数
@beginindex int=1,--记录开始数
@docount bit =0 --是否统计总数,为0不统计,为1统计
-- @pageindex int =1 --当前页
--@classcode char(10) =null, --单位编号(班级编号)
as
begin
set nocount on;
declare @sql nvarchar(4000);
declare @totalrecord int;
--计算总记录数
if (@strwhere ='''' or @strwhere='' or @strwhere is null)
set @sql = 'select @totalrecord = count(*) from ' + @strfrom
else
set @sql = 'select @totalrecord = count(*) from ' + @strfrom + ' where ' + @strwhere
exec sp_executesql @sql,n'@totalrecord int output',@itemcount output--计算总记录数
declare @sqlquery varchar(max)
-- if(@pageindex=1)
if(@beginindex=1 or @beginindex=0 or @beginindex begin
if(@strwhere is null)--if(@strwhere='')
set @sqlquery='select top '+convert(varchar,@pagesize)
+ ' row_number() over(order by '+@strorder+' ) as rownumber,'+@strselect+
' from '+@strfrom;
else
--set @sql='select top @pagesize * from @tablename order by id desc';
--select top @pagesize * from @tablename order by id desc;
set @sqlquery='select top '+convert(varchar,@pagesize)
+ ' row_number() over(order by '+@strorder+' ) as rownumber,'+@strselect+' from '+@strfrom+' where '+@strwhere;
--exec (@sqlquery)
-- @sqlquery
end
else
begin
if(@strwhere is null)--if(@strwhere='')
begin
set @sqlquery='with cte as (
select row_number() over(order by '+@strorder+' ) as rownumber,'+@strselect+' from '+@strfrom+'
)
select * from cte where rownumber between '+
--convert(varchar,((@pageindex-1)*@pagesize)+1)+' and '+
--
-- convert(varchar,@pageindex*@pagesize)
convert(varchar,@beginindex)+' and '+
convert(varchar,@beginindex+@pagesize)
--print @sqlquery
end
else
begin
set @sqlquery='with cte as (
select row_number() over(order by '+@strorder+' ) as rownumber,'+@strselect+' from '+@strfrom+' where '+@strwhere+'
)
select * from cte where rownumber between '+
--convert(varchar,((@pageindex-1)*@pagesize)+1)+' and '+
--
-- convert(varchar,@pageindex*@pagesize)
convert(varchar,@beginindex)+' and '+
convert(varchar,@beginindex+@pagesize)
--print @sqlquery
end
end
--set @sqlquery=@sqlquery+';select @itemcount =count(*) from '+@tablename
--set @pagecount=@itemcount/@pagesize
--print '共'+@pageconut+'页'+@itemcount+'条'
--print @itemcount
print @sqlquery
exec (@sqlquery)
end
c#相关代码的访问使用的是微软的企业库 v4.1
 enterprise library 4.1 下载地址:
http://www.microsoft.com/downloads/details.aspx?familyid=1643758b-2986-47f7-b529-3e41584b6ce5&displaylang=en
示例代码,前台页面,前台为用户控件
复制代码 代码如下:
单位:
级别:级节点
该单位共有学员

每页显示onselectedindexchanged=ddlpagesize_selectedindexchanged>
人 共页
现为第页
oncommand=linkbutton_command>首页
oncommand=linkbutton_command>下一页
oncommand=linkbutton_command>上一页
oncommand=linkbutton_command>末页
emptydatatext=没有符合条件的数据>
示例代码,后台代码
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.text;
using system.data;
using system.data.common;
using microsoft.practices.enterpriselibrary.common;
using microsoft.practices.enterpriselibrary.data;
using kimbanx.ucs.foreignstudentadmin.model;
using kimbanx.ucs.foreignstudentadmin.common;
namespace kimbanx.ucs.foreignstudentadmin.usercontrol.usercontrol
{
public partial class studentdetailstable : system.web.ui.usercontrol
{
private database _db = databasefactory.createdatabase();
private dbcommand _command;
private dbconnection _connection;
private dataset _ds;
private string _classcode;
private string _classfullname;
private string _studenttype;
private string _studentcount;
private string _querystringwhere;
private datatable _studenttable;
protected string setbirthdate(object obj)
{
string result = string.empty;
string temp = obj.tostring();
result = datetime.parse(temp).toshortdatestring();
return result;
}
protected string setenrolldate(object obj)
{
string result = string.empty;
string temp = obj.tostring();
result = datetime.parse(temp).toshortdatestring();
return result;
}
protected void filldata_dllpagesize()
{
for (int i = 1; i {
ddlpagesize.items.add(i.tostring());
}
ddlpagesize.selectedindex = 14;
}
protected void initsession()
{
//session[pagesize] = 0;
session[pageindex] = 1;
session[pagecount] = int.parse(_studentcount) / 15 + 1;
}
///
/// 获取querystring传递参数
///
protected void getquerystringpara()
{
_classcode = request.querystring[dwbh];
_classfullname =httputility.urldecode( request.querystring[dwmc]);
_studentcount = request.querystring[studentcount];
_studenttype =httputility.urldecode( request.querystring[studenttype]);
_querystringwhere = request.querystring[where];
}
protected void setlabeltext()
{
this.lblclassname.text = _classfullname;
this.lblclasslevel.text = getclassinfo(_classcode).level.tostring();
this.lblstudentcount.text = _studentcount;
this.lblstudenttype.text = _studenttype;
}
#region
/////
///// 获取学员数据
/////
///// 显示的字段
///// 用到的
/////查询条件
///// 每页显示条数
///// 当前页
/////
//protected datatable getstudentdata(string strselect,string strfrom,string strwhere,int pagesize,int pageindex)
//{
// _command = _db.getstoredproccommand(studentpaging);
// _db.addinparameter(_command, strselect, dbtype.string, zpadress,xmjz,xmjy,jx,zw,gj,sjyqk,zj,csrq,rwrq,xzz,dhd,dhx,fcjp,hzh,xh);
// _db.addinparameter(_command, strfrom, dbtype.string, tx_xyzl);
// _db.addinparameter(_command, strwhere, dbtype.string, strwhere );
// _db.addinparameter(_command, strorder, dbtype.string, id);
// _db.addinparameter(_command, pagesize, dbtype.int32, pagesize );
// _db.addinparameter(_command, pageindex, dbtype.int32,pageindex );
// _studenttable = _db.executedataset(_command).tables[0];
// return _studenttable;
/
其它类似信息

推荐信息