分页存储过程代码

2016-02-19 10:32 0 1 收藏

下面这个分页存储过程代码教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - 编程语言 】

代码如下:

/*
*@curentpage 当前页
*@pagesize 每页记录数
*@TableName 表名
*@key 主键(自动排序)
*@where 查询条件
1)空为 null
2)有查询条件不要带where
*@order '0'表示 desc '1'是asc
*@pageCount 总页数
*/
create procedure Page
@currentpage int,@pagesize int,
@TableName varchar(30),@key varchar(30),
@where varchar(50),@order varchar(1),
@pageCount int ,@str varchar(450) output
as
begin
---------------执行的sql语句------------
declare @sql nvarchar(400),@ordreby nvarchar(200)
declare @tempsql1 varchar(200),@tempsql2 varchar(200)
---------------记录总数-----------------
declare @count int
---------------临时变量------------------------
declare @temp1 int,@temp2 int

set @TableName=' '+@TableName+' '
set @key=' '+@key+' '

if @order='0'
set @ordreby=' order by '+@key+'desc'
else
set @ordreby=' order by '+@key

if @where='null'
set @sql='select @count = count(*) from '+ @TableName
else
set @sql='select @count = count(*) from '+ @TableName+' where '+@where

------------@count 付值(声明变量@count 在说明是output 内型)---------------------------
exec sp_executesql @sql,N'@count int out',@count out
------------求总页数------------------------------
if (@count%@pagesize)=0
set @pagecount=@count/@pagesize
else
set @pagecount=@count/@pagesize+1
-----------判断显示当前页是否异常------------------
if @currentpage@pagecount
set @currentpage=@pagecount
if @currentpage1
set @currentpage=1
----------记录数小于页面显示记录数-----------------
if(@currentpage=1)
begin
if @where='null'
set @where=' '
else
set @where=' where '+@where
set @sql = 'select top'+ str(@pagesize)+' * from '+@TableName+@where+@ordreby
end
else
begin
/**//* ---------------desc----------------------
*@temp1表示前面的记录
*@temp2表示后面的记录
*假设一共77个记录,每次取10个。取67~58(第2页),去掉前面的57(1~57)个和后面的10个(77~66)
*/
if @order=0
begin
set @temp1 = @count-@currentpage*@pagesize
if @temp10
set @temp1=0
set @temp2 = (@currentpage - 1)*@pagesize
if @where='null'
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName + @ordreby
end
else
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' where '+@where+' order by ' +@key
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName+' where '+@where+@ordreby
end
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
set @sql= @sql+' ( '+ @tempsql1 +' ) and '
set @sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby
end
/**//* ----------------asc---------------------
* @temp 表示前面显示的记录总数
* 去掉 @temp 在取出 pagesize 个即可
*/
else
begin
set @temp1=(@currentpage-1)*@pagesize
if @where='null'
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName + @ordreby
else
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName ++' where '+@where+@ordreby
set @sql=' select top ' + str(@pagesize) + ' * from ' + @TableName + ' where '+@key+ ' not in '
set @sql=@sql+' ( '+@tempsql1+' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby

end
/**//* -------------------------------------*/
end
set @str=@sql
--exec sp_executesql @sql

end

GO

来源:https://www.tulaoshi.com/n/20160219/1594987.html

延伸阅读
/* *************************************************************** ** 中国无忧商务网千万数量级分页存储过程 ** *************************************************************** 参数说明: 1.Tables :表名称,视图 2.PrimaryKey :主关键字 3.Sort :排序语句,不带Order&nbs...
其实在很多时候设计的度还是要把握的,不至于让自己陷入的怪圈中才是最重要的,因为我们还要留出时间还解决其他的很多问题,个人认为适度就可以了,留出一定的空间。也因为万能是不存在的,万物在一定的范畴之内都是合理的,出了范畴可能就没有合理的了。          分页存储过程大致有下列几种 1、&n...
标签: Web开发
建立一个 Web 应用,分页浏览功能必不可少。这个问题是数据库处理中十分常见的问题。经典的数据分页方法是:ADO 纪录集分页法,也就是利用ADO自带的分页功能(利用游标)来实现分页。但这种分页方法仅适用于较小数据量的情形,因为游标本身有缺点:游标是存放在内存中,很费内存。游标一建立,就将相关的记录锁住,直到取消游标...
标签: ASP
  /*****听以前的同事说asp页面上的分页太慢了(如果数据多了), 就想了这么个笨办法。有些地方还要考虑----比如select top 22 * from cat_list where T_id not in (select T_id from #change)是否有效率问题;数据不能重复等等 不过灵活性挺好。希望各位高手再给帮忙改正;多谢chair3的帮助---这个存储过程还可以在加入几个变量,随便...
标签: ASP
  在  网上 讨论 如何 实现 分页  有很多程序,我在这里向大家  介绍一种实现分页的新的方法,使用 存储过程 来实现分页    由于 这段程序写的 比较早,那个时候 还没有 SQL 7,每一个 Varchar 只能 支持 255 个字符,所以 采取了一种比较笨的办法,如果大家有兴趣,请去  http:/...

经验教程

424

收藏

86
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部