cursor游标(mysql)
- /*
- 游标 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()
- begin
- declare 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()
- begin
- declare 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()
- begin
- declare 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()
- begin
- declare 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;
- repeat
- fetch 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()
- begin
- declare 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 do
- fetch 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()
- begin
- declare 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 do
- fetch 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()
- begin
- declare 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 do
- fetch 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)的更多相关文章
- 【PLSQL】变量声明,结构语句,cursor游标
************************************************************************ ****原文:blog.csdn.net/clar ...
- 转 oracle cursor 游标
转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念: 游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...
- DRF url控制 解析器 响应器 版本控制 分页(常规分页,偏移分页,cursor游标分页)
url控制 第二种写法(只要继承了ViewSetMixin) url(r'^pub/$',views.Pub.as_view({'get':'list','post':'create'})), #获取 ...
- mysql cursor游标的使用,实例
mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...
- 存储过程/游标/mysql 函数
存储过程和函数(存储在 mysql数据库中的 proc表,所以检查有没有这个表)存储过程是一种存储程序(如正规语言里的子程序一样),mysql支持有两种:存储过程,在其他SQL语句中可以返回值的函数( ...
- SQL SERVER CURSOR游标的使用(转载)
一:认识游标 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据 ...
- SQL Cursor 游标的使用
DECLARE @name VARCHAR(50) --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...
- SQL Cursor(游标)
1.游标在数据表没有id(identity(1,1))时好用,但是游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量 2.如果能不用游标,尽量不要使用游标,用完用完之后一定要 ...
- 关键字(5):cursor游标:(循环操作批量数据)
declare cursor stus_cur is select * from students; --定义游标并且赋值(is 不能和cursor分开使用) cur_stu studen ...
随机推荐
- path.resolve()和path.join()
resolve 作用:path.resolve() 该方法将一些的 路径/路径段 解析为绝对路径. 语法:path.resolve([...paths]) 说明: ...paths <strin ...
- Camera帧率和AE的关系
1.camera首先是通过曝光的pixel加上dummy pixel以及曝光的line加上dummy line来决定一帧的曝光时间,这一帧曝光时间的倒数就是帧率,这个没有错吧,但是看代码时候看到pre ...
- python---自动群发邮件
生活中我们经常发送邮件,那么我们能不能用Python写一个自动发送邮件的功能呢?答案是肯定的!!! 开始实现功能之前我们需要开启我们邮箱的 IMAP/SMTP功能,我们先了解一下什么是IMAP/SMT ...
- 照着官网来安装openstack pike之nova安装
nova组件安装分为控制节点和计算节点,还是先从控制节点安装 1.前提条件,数据库为nova创建库和账户密码来连接数据库 # mysql -u root -p MariaDB [(none)]> ...
- vsftpd基于mysql的认证方式
安装epel源: cd /etc/yum.repos.d wget http://mirrors.neusoft.edu.cn/epel/epel-release-latest-6.noarch.rp ...
- 20144303 《Java程序设计》第一周学习总结
20144303 <Java程序设计>第一周学习总结 教材学习内容总结 下载.安装.调试了JDK. JavaSE是各语言个应用平台的基础,分为四个主要的部分:JVE,JRE,JDK,和ja ...
- 原生的 promise 的局限性
本文来自:https://ekyu.moe/article/limits-of-native-promise-and-async-await/ 众所周知,Nodejs 已原生支持 Promise 和 ...
- [BZOJ1058]报表统计
Description 小Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工 作,作为她的生日礼物之一.经过仔细观察,小Q发现统计一张报表实际上是维护一个 ...
- 爬虫之Requests: 让 HTTP 服务人类
Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...
- AngularJS Injector和Service的工作机制
要了解angularJS里的injector和Service是如何工作的,需要阅读/src/auto/injector.js.另外要结合/src/loader.js才能明白它的应用场景. auto/i ...