• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > 分页存储过程代码

分页存储过程代码

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了分页,存储过程等相关知识,希望本文的分享对您有所帮助

一个分页存储过程分享

代码如下:
/*
*@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 @currentpage<1
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 @temp1<0
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
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 基于mysql查询语句的使用详解
  • MySQL查询优化:连接查询排序limit(join、order by、limit语句)介绍
  • MySQL与Oracle的语法区别详细对比
  • MSSQL根据ID进行分页实现方法
  • MySQL 百万级分页优化(Mysql千万级快速分页)
  • SQL语句详解 MySQL update的正确用法
  • mysql部分替换sql语句分享
  • mysql SQL语句积累
  • mysql 查询第几行到第几行记录的语句
  • Mysql limit 优化,百万至千万级快速分页 复合索引的引用并应用于轻量级框架

相关文章

  • 2018-12-05MySQL 数据库 source 命令详解_MsSql
  • 2018-12-0521个优化MySQL的建议
  • 2018-12-05mysql GTID主从复制详解
  • 2018-12-05Mysql存储引擎之MyISAM存储引擎的介绍
  • 2018-12-05sqlserver 存储过程分页代码第1/2页
  • 2018-12-05麦子学院深入浅出 redis 视频资料分享
  • 2018-12-05解决MSSQL2005远程连接sql2000非默认端口数据库的问题
  • 2017-05-11MySQL查询全部数据集结果不一致问题解决方案
  • 2018-12-05MYSQL经典语句大全——提升篇
  • 2017-05-11深入mysql外键关联问题的详解

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • 库名表名大小写问题与sqlserver兼容的启动配置方法
    • MySQL数据表的创建、查看、插入实例详解
    • 深入Mysql字符集设置分析
    • 了解数据库设计三大范式
    • Last_Errno: 1062,Last_Error: Error Duplicate entry
    • 貌似很强的mysql备份策略分享
    • Oracle数据库及应用程序优化开发者网络Oracle
    • MySQL order by性能优化方法实例
    • 运行asp.net程序 报错:磁盘空间不足
    • MySQL 5.5 DBA工具 多进程dump 多进程load 多进程备份还原 Pytho

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有