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 ...
随机推荐
- java反射 - getXXX 与 getDeclaredXXX
1.getXXX 和 getDeclaredXXX java 里 Class<?> 有下面这些方法: 类似的方法有: 2.getMethod(s) 和 getDeclaredMethod( ...
- 20145302张薇《Java程序设计》第十六周课程总结
20145302 <Java程序设计>第十六周课程总结 实验报告链接汇总 实验一 Java开发环境的熟悉 实验二 Java面向对象程序设计 实验三 敏捷开发与XP实践 实验四 Andoid ...
- 《Java程序设计》实验1实验报告
20145318 <Java程序设计>实验1实验报告 实验题目 通过对500个数据进行操作,实现快速排序.选择排序.直接插入排序算法时间复杂度的比较:并在排序数据中快速查找某一数据,给出查 ...
- 20145240《网络对抗》Web基础
Web基础 实验后回答问题 什么是表单? 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...
- Mybatis中使用自定义的类型处理器处理枚举enum类型
知识点:在使用Mybatis的框架中,使用自定义的类型处理器处理枚举enum类型 应用:利用枚举类,处理字段有限,可以用状态码,代替的字段,本实例,给员工状态字段设置了一个枚举类 状态码,直接赋值给对 ...
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- android的wifi程序随笔作业
不用说,做前最好新建一个wifiadmin类,用来装载你所有的wifi打开关闭,wifi配置,连接情况等等wifi操作,然后main类里做一些button连接listview显示wifi网络连接等东西 ...
- try throw catch typeid
QString str = ui.ll->text(); try { if (str == NULL) { throw 1; } else { throw 1.2; } } catch (int ...
- ThinkPHP开发笔记-前后端数据交互
此处就是 Controller 和 View 相互传数据. 1.Controller 向 View 的页面传数据.在控制器中把变量传递给模板,使用 assign 方法对模板变量赋值.例如: 在Cont ...
- centos7 systemctl一些用法
systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enabled servicename.service #查询服务是否 ...