/*
游标 cursor
什么是游标?为什么需要游标
使用存储过程对sql进行编程的时候,我们查询的语句可能是数据是多个,它总是一口气全部执行,我们无法针对每一条进行判断。也就是说,我们无法控制程序的运行,所以引入了游标cursor
cursor类似于java中的迭代器。  它利用查询语句生成一个游标,然后游标中有一个类似指针的东西。首先指在游标首,就是迭代器。不解释了cursor 游标declare声明;  declare 游标名 cursor for select_statement;open 打开; open游标名fetch 取值; fetch 游标名 into var1,var2[,...]   select语句中查出的项有多少,就需要使用多少变量接受close 关闭; close 游标名
*/create table goods
(id int,name varchar(20),num int
);
insert into goods values (1,'dog',20),(2,'cat',30),(3,'pig',25);
select * from goods;-- 游标在存储过程中使用drop procedure p1;
create procedure p1()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量open gs;fetch gs into row_id,row_name,row_num;select row_id,row_name,row_num;close gs;end;call p1();create procedure p2()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量open gs;fetch gs into row_id,row_name,row_num;fetch gs into row_id,row_name,row_num;fetch gs into row_id,row_name,row_num;select row_id,row_name,row_num;close gs;end;call p2(); --报错,如果取出游标数据的个数超过游标中数据的个数,报错。类似于数组越界drop procedure p3;
create procedure p3()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare gs cursor for select id,name,num from goods; -- 声明游标的语句后面不能有声明变量open gs;fetch gs into row_id,row_name,row_num;select row_id,row_name,row_num;fetch gs into row_id,row_name,row_num;select row_id,row_name,row_num;fetch gs into row_id,row_name,row_num;select row_id,row_name,row_num;close gs;end;
call p3();--学会使用循环控制试试
create procedure p4()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare count_r int;declare i int default 0;declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面select count(*) into count_r from goods;open gs;repeatfetch gs into row_id,row_name,row_num;select row_id,row_name,row_num; set i := i+1;until i>=count_r end repeat;close gs;
end;call p4();  -- 用while循环试试
create procedure p5()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare count_r int;declare i int default 0;declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面select count(*) into count_r from goods;open gs;while i<count_r dofetch gs into row_id,row_name,row_num;select row_id,row_name,row_num; set i := i+1;end while;close gs;
end;call p5();  -- 使用游标最主要的是可以针对每一次查出来的结果进行一些操作
drop procedure p6;
create procedure p6()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare count_r int;declare i int default 0;declare gs cursor for select id,name,num from goods;  -- 游标声明语句好像位置有限定。不能在声明变量前面,不能再哎select语句后面select count(*) into count_r from goods;open gs;while i<count_r dofetch gs into row_id,row_name,row_num;if row_num>25 then select concat(row_name,'比较多'); elseif row_num=25 then select concat(row_name,'刚刚好');else select concat(row_name,'有点少');end if;        set i := i+1;end while;close gs;
end;call p6();-- 第三种方式:游标越界时候使用标志,利用标识来结束
-- 在mysql cursor中,可以使用declare continue handler来操作一个越界标识
-- declare continue handler for not found statement;
drop procedure p7;
create procedure p7()
begindeclare row_id int;declare row_name varchar(20);declare row_num int;declare you int default 1;declare gs cursor for select id,name,num from goods;declare continue handler for not found set you:=0;open gs;while you!=0 dofetch gs into row_id,row_name,row_num;if you!=0 then select row_num,row_name;end if;      end while;close gs;
end;
call p7();

cursor游标(mysql)相关推荐

  1. 【数据库学习笔记】——cursor游标对象

    目录 1.创建cursor对象 2.cursor对象常用方法 3.操作数据库的常见流程(五部曲) 课程视频链接: 第14节 Python操作数据库_哔哩哔哩_bilibili666https://ww ...

  2. DRF url控制 解析器 响应器 版本控制 分页(常规分页,偏移分页,cursor游标分页)...

    url控制 第二种写法(只要继承了ViewSetMixin) url(r'^pub/$',views.Pub.as_view({'get':'list','post':'create'})), #获取 ...

  3. Oracle中cursor(游标)总结

    1.       游标: 容器,存储SQL语句影响行数. 2.       游标类型: 隐式游标,显示游标,REF游标.其中,隐式游标和显示游标属于静态游标(运行前将游标与SQL语句关联),REF游标 ...

  4. django mysql 游标,MySQL Cursor 存储过程之游标与相关循环

    简单介绍游标 在检索出来的行中,前进或者后退一行或多行,就需要用到所谓的"游标". 游标不是某个SELECT语句,但是它是被该语句检索出来的结果集. 几个特点: ·MySQL游标只 ...

  5. mysql cursor使用变量_mysql cursor游标的使用,实例

    mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...

  6. Mysql while 嵌套 cursor 游标, 数据迁移

    CREATE DEFINER=`root`@`%` PROCEDURE `removeAccPartnerSettle`() BEGIN#Routine body goes here...declar ...

  7. python cursor游标重置位置scroll_MySQL的游标

    python操作mysql 安装 python操作mysql数据库,主要就是通过pymysql模块 pip install pymysql 操作流程 1)建立数据库连接对象 conn 2)通过 con ...

  8. python cursor游标_第二十三天 python中游标的使用

    游标(cursor):系统为用户开通的一个数据缓冲区,用于存放SQL语句执行结果.用户使用的sql会逐一的在游标中获取记录,并赋值给主变量,交由Python进一步处理,一组主变量只能存放一条记录. 例 ...

  9. mysql函数 游标_存储过程/游标/mysql 函数

    存储过程和函数(存储在 mysql数据库中的 proc表,所以检查有没有这个表) 存储过程是一种存储程序(如正规语言里的子程序一样),mysql支持有两种:存储过程,在其他SQL语句中可以返回值的函数 ...

最新文章

  1. 天猫国际618一骑绝尘,占中国跨境进口电商总订单超七成
  2. win7 32 安装mongoDB遇到的问题
  3. 机器人聊天软件c#_C#制作简易QQ聊天机器人
  4. 关于引用传递的测试题
  5. java 右键事件_java table右键点击事件
  6. 后缀的形容词_高三语法总复习:名词形容词变名词的后缀
  7. 通过IHttpHandlerFactory,过滤TextBox、Input和Textarea中的特殊字符
  8. 3D游戏建模就是那么简单
  9. 驱动中的资源共享和临界代码保护
  10. compoundbutton(compoundbutton是什么意思)
  11. 【javaEE】——多线程进阶(锁策略:面试相关考点)04
  12. ps还原上一步快捷键,ps返回上一步快捷键是什么
  13. 拿下多家主机厂数百万前装定点,禾赛科技激光雷达量产进程加速
  14. 笔记本处理器排名_上半年最受欢迎处理器TOP10榜单:AMD终进榜,9代酷睿无缘前10...
  15. 【二次开发教程】Ai-WB2系列的eclipes搭建环境教程
  16. 数字信号内插方法的python实现
  17. android 7.0 2g,LG美国定制机曝光:2GB内存,搭载安卓7.0系统
  18. 安全(Security)设计原则(1)
  19. 徐无忌并发编程笔记:无锁机制CAS及其底层实现原理?
  20. “鲲鹏”七夕坐“高铁”,这难道是个灯谜?

热门文章

  1. 【Linux】磁盘分区
  2. SharePoint 创建 Lookup 类型的Site Column解决跨站问题
  3. IDEA导入Git项目后右键项目找不到Git选项的解决方法
  4. Tomcat异常:The Tomcat server configuration at\Servers\Tomcat v9.0 Server at localhost-c
  5. Android studio R文件丢失或错误解决方法
  6. ThreadLocal 遇上线程池的问题及解决办法
  7. Notice: Undefined offset 的解决方法
  8. 在bash中仅使用l列出目录:检查
  9. 如何将道具传递给{this.props.children}
  10. 华为 博士 实习_华科女博士年薪156入职华为!最新回应:在深圳也难买房…