/*
游标 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)的更多相关文章

  1. 【PLSQL】变量声明,结构语句,cursor游标

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  2. 转 oracle cursor 游标

    转自:http://blog.csdn.net/liyong199012/article/details/8948952 游标的概念:     游标是SQL的一个内存工作区,由系统或用户以变量的形式定 ...

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

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

  4. mysql cursor游标的使用,实例

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

  5. 存储过程/游标/mysql 函数

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

  6. SQL SERVER CURSOR游标的使用(转载)

    一:认识游标 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集. 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式. 用SQL语言从数据库中检索数据 ...

  7. SQL Cursor 游标的使用

    DECLARE @name VARCHAR(50)  --声明游标 DECLARE cursor_VAA1 CURSOR FOR SELECT VAA05 FROM VAA1 --打开游标 OPEN ...

  8. SQL Cursor(游标)

    1.游标在数据表没有id(identity(1,1))时好用,但是游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量 2.如果能不用游标,尽量不要使用游标,用完用完之后一定要 ...

  9. 关键字(5):cursor游标:(循环操作批量数据)

    declare   cursor stus_cur is select * from students;  --定义游标并且赋值(is 不能和cursor分开使用)    cur_stu studen ...

随机推荐

  1. Greatest Common Increasing Subsequence

    /*HDU1423 最长公共递增*/ #include <stdio.h> #include <string.h> #include <iostream> usin ...

  2. Django----Request对象&Response对象

    Django 使用Request 对象和Response 对象在系统间传递状态. HttpRequest 对象: Request.body:一个字节字符串,表示原始HTTP 请求的正文.它对于处理非H ...

  3. 再也不学AJAX了!(一)AJAX概述

    "再也不学AJAX了"是一个与AJAX主题相关的文章系列,包含以下三个部分的内容: AJAX概述:主要回答"AJAX是什么"这个问题: 使用AJAX:介绍如何通 ...

  4. Java 面试题代码类收集

    long temp=(int)3.9; System.out.println(temp); temp%=2; System.out.println(temp); 3 1 Java里面类的方法名可以与类 ...

  5. filebeat 乱码

    查看 文件的类型 [root@elk-node-1 rsyslog] # file 192.168.1.16.log 192.168.1.16.log: Non-ISO extended-ASCII ...

  6. Vue中使用百度地图——根据输入框输入的内容,获取详细地址

    知识点:在Vue.js项目中调用百度地图API,实现input框,输入地址,在百度地图上定位到准确地址,获得到经纬度 参考博客:  百度地图的引用,初步了解参考博客:http://blog.csdn. ...

  7. SSH免密码登录Linux

    如果两台linux之间交互频繁,但是每次交互如果都需要输入密码,就会很麻烦,通过配置SSH就可以解决这一问题 下面就说下配置流程(下面流程在不同机器上全部操作一边) 1)cd ~到这个目录中 2)ss ...

  8. MongoTemplate WriteResult acknowledged=false 的问题

    今天使用 MongoTemplate 的 update 操作时,发现 WriteResult 的 acknowledged 一直为 false ,个人首先想到可能时java驱动版本不对,在更换好对应版 ...

  9. Android studio 2.3安装遇到的问题

    我的安装系统环境:windows 10+jdk1.8.0_111 . 1.在安装Android studio 2.3之前,请安装最新的java jdk.Android studio的安装包里是不包含j ...

  10. filezilla无法启动传输及严重文件传输错误

    filezilla无法启动传输 严重文件传输错误 文件夹权限不够,修改之. 你的空间或服务器已经满了,请空下回收站或者扩容. 文件正在被占用,关闭后传输 ​