mysql 游标的用法和作用,话不多说,这个是网上看到的例子,简答粗暴。

例子:

当前有三张表a、b、c其中a和b是一对多关系,b和c是一对多关系,现在需要将b中a表的主键存到c中;

常规思路就是将b中查询出来然后通过一个update语句来更新c表就可以了,但是b表中有2000多条数据,

难道要执行2000多次?显然是不现实的;最终找到写一个存储过程然后通过循环来更新c表,

然而存储过程中的写法用的就是游标的形式。

简介

游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。

游标充当指针的作用。(有'C'的味道了)

尽管游标能遍历结果中的所有行,但他一次只指向一行。

游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作。

用法

一、声明一个游标: declare 游标名称 CURSOR for table;(这里的table可以是你查询出来的任意集合)

二、打开定义的游标:open 游标名称;

三、获得下一行数据:FETCH 游标名称 into testrangeid,versionid;

四、需要执行的语句(增删 改查):这里视具体情况而定

五、释放游标:CLOSE 游标名称;

实例

-

BEGIN

--定义变量

declare testrangeid BIGINT;

declare versionid BIGINT;

declare done int;

--创建游标,并存储数据

declare cur_test CURSOR for

select id as testrangeid,version_id as versionid from tp_testrange;

--游标中的内容执行完后将done设置为1

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

--打开游标

open cur_test;

--执行循环

posLoop:LOOP

--判断是否结束循环

IF done=1 THEN

LEAVE posLoop;

END IF;

--取游标中的值

FETCH cur_test into testrangeid,versionid;

--执行更新操作

update tp_data_execute set version_id=versionid where testrange_id = testrangeid;

END LOOP posLoop;

--释放游标

CLOSE cur_test;

END

-

例子2:

--在windows系统中写存储过程时,如果需要使用declare声明变量,需要添加这个关键字,否则会报错。

delimiter //

drop procedure if exists StatisticStore;

CREATE PROCEDURE StatisticStore()

BEGIN

--创建接收游标数据的变量

declare c int;

declare n varchar(20);

--创建总数变量

declare total int default 0;

--创建结束标志变量

declare done int default false;

--创建游标

declare cur cursor for select name,count from store where name = 'iphone';

--指定游标循环结束时的返回值

declare continue HANDLER for not found set done = true;

--设置初始值

set total = 0;

--打开游标

open cur;

--开始循环游标里的数据

read_loop:loop

--根据游标当前指向的一条数据

fetch cur into n,c;

--判断游标的循环是否结束

if done then

leave read_loop; --跳出游标循环

end if;

--获取一条数据时,将count值进行累加操作,这里可以做任意你想做的操作,

set total = total + c;

--结束游标循环

end loop;

--关闭游标

close cur;

--输出结果

select total;

END;

--调用存储过程

call StatisticStore();

fetch是获取游标当前指向的数据行,并将指针指向下一行,当游标已经指向最后一行时继续执行会造成游标溢出。

使用loop循环游标时,他本身是不会监控是否到最后一条数据了,像下面代码这种写法,就会造成死循环;

read_loop:loop

fetch cur into n,c;

set total = total+c;

end loop;

在MySql中,造成游标溢出时会引发mysql预定义的NOT FOUND错误,所以在上面使用下面的代码指定了当引发not found错误时定义一个continue 的事件,

指定这个事件发生时修改done变量的值。

declare continue HANDLER for not found set done = true;

--判断游标的循环是否结束

if done then

leave read_loop; --跳出游标循环

end if;

如果done的值是true,就结束循环。继续执行下面的代码

使用方式

第一种就是上面的实现,使用loop循环;

第二种方式如下,使用while循环:

drop procedure if exists StatisticStore1;

CREATE PROCEDURE StatisticStore1()

BEGIN

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

fetch cur into n,c;

while(not done) do

set total = total + c;

fetch cur into n,c;

end while;

close cur;

select total;

END;

call StatisticStore1();

第三种方式是使用repeat执行:

drop procedure if exists StatisticStore2;

CREATE PROCEDURE StatisticStore2()

BEGIN

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

repeat

fetch cur into n,c;

if not done then

set total = total + c;

end if;

until done end repeat;

close cur;

select total;

END;

call StatisticStore2();

游标嵌套

在mysql中,每个begin end 块都是一个独立的scope区域,由于MySql中同一个error的事件只能定义一次,如果多定义的话在编译时会提示Duplicate handler declared in the same block。

drop procedure if exists StatisticStore3;

CREATE PROCEDURE StatisticStore3()

BEGIN

declare _n varchar(20);

declare done int default false;

declare cur cursor for select name from store group by name;

declare continue HANDLER for not found set done = true;

open cur;

read_loop:loop

fetch cur into _n;

if done then

leave read_loop;

end if;

begin

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'iphone';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

iphone_loop:loop

fetch cur into n,c;

if done then

leave iphone_loop;

end if;

set total = total + c;

end loop;

close cur;

select _n,n,total;

end;

begin

declare c int;

declare n varchar(20);

declare total int default 0;

declare done int default false;

declare cur cursor for select name,count from store where name = 'android';

declare continue HANDLER for not found set done = true;

set total = 0;

open cur;

android_loop:loop

fetch cur into n,c;

if done then

leave android_loop;

end if;

set total = total + c;

end loop;

close cur;

select _n,n,total;

end;

begin

end;

end loop;

close cur;

END;

call StatisticStore3();

动态SQL

Mysql 支持动态SQL的功能

set @sqlStr='select * from table where condition1 = ?';

prepare s1 for @sqlStr;

--如果有多个参数用逗号分隔

execute s1 using @condition1;

--手工释放,或者是 connection 关闭时, server 自动回收

deallocate prepare s1;

mysql游标的用法及作用_Mysql 游标的用法及其作用相关推荐

  1. 查看mysql view作用_Mysql中View视图的作用

    浙江PHP博客分享关于Mysql中使用View视图的作用.很多小伙伴知道视图的作用,却不知道为什么要使用视图以及视图的一些好处是什么,下面浙江一点PHP将详细阐述关于视图的用法以及作用. 作用一: 提 ...

  2. mysql 临时表的作用_mysql临时表的用法

    当处理较复杂大的逻辑时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录存到一个临时表可能更快些,然后多这些表运行查询.这 ...

  3. mysql show作用_MySQL show的用法

    a. show tables或show tables from database_name; // 显示当前数据库中所有表的名称 b. show databases; // 显示mysql中所有数据库 ...

  4. mysql 变量作用_MySQL变量的用法

    在编写存储过程中,有时需要使用变量,保存数据处理过程中的值 MySQL中,变量可以在子程序中,声明并使用,作用范围在BEGIN--END程序中 定义变量 在存储过程中,使用DECLARE语句,定义变量 ...

  5. mysql leave的作用_MySQL数据库中DELIMITER的作用

    以下的文章主要是向大家描述的是MySQL数据库中delimiter的作用是什么?我们一般都认为这个命令和存储过程关系不大,到底是不是这样的呢?以下的文章将会给你相关的知识,望你会有所收获. 下面是一个 ...

  6. mysql delimiter的作用_MySQL数据库中delimiter的作用概述

    以下的文章主要是向大家描述的是MySQL数据库中delimiter的作用是什么?我们一般都认为这个命令和存储过程关系不大,到底是不是这样的呢?以下的文章将会给你相关的知识,望你会有所收获. 其实就是告 ...

  7. mysql 慢日志 作用_MySQL慢查询日志的作用和开启

    前言 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中.lon ...

  8. mysql 慢查询日志的作用_MySQL慢查询日志的作用和开启

    前言 MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中.lon ...

  9. mysql中 top是什么意思_mysql top的用法是什么

    我就废话不多说了,大家还是直接看代码吧~create or replace function aa1(a1 integer[],a2 bigint) returns void AS $$declare ...

最新文章

  1. [bzoj2839]集合计数 题解 (组合数+容斥)
  2. 来自51CTOHCNP3期一位技术小白的内心独白
  3. Android 4.4 Kitkat 使能有线网络 Ethernet
  4. SwiftUI3优秀文章 NavigationLink图片和文字显示蓝色或者图片无显示
  5. uva 10037——Bridge
  6. mysql 存储过程 循环结构 命名_mysql存储过程----循环结构
  7. pyramid新建项目
  8. [ZJOI2010]贪吃的老鼠
  9. 震惊!Fibonacci Again
  10. 格密码教程(四):SVP和CVP,Hermite定理,Blichfeld定理和Minkowski定理
  11. Django数据获取操作
  12. 实例:评审速度与缺陷密度之间的相关性
  13. activesync同步问题
  14. 【5G学习笔记-8】38.306 36.306 User Equipment (UE) radio access capabilities 以及终端CDRX能力上报 featureGroupIndic
  15. Redux开发实用教程
  16. 什么是RS232电平?什么是TTL电平?
  17. matlab怎么计算行列式,MATLAB计算行列式
  18. java阿里天气接口_天气预报接口
  19. PAT1003 我要通过! (20 分)(C语言)
  20. 小学计算机学校教学计划,小学信息技术课程教学计划

热门文章

  1. java web逻辑删除代码_MyBatis-Plus之逻辑删除的实现
  2. kotlin字符串数组_Kotlin程序读取,遍历,反向和排序字符串数组
  3. 为什么ConcurrentHashMap不允许插入null值?
  4. 漫画:什么是归并排序?
  5. react native一键分享功能实现amp;原理和注意点(支持微信、qq、新浪微博等)
  6. Net操作Excel(终极方法NPOI)
  7. jquery的扩展方法介绍
  8. uni-calendar更改打点颜色实现签到和缺勤不同打点颜色效果
  9. python asyncio 并发编程_asyncio并发编程
  10. 平流式初沉池贮砂斗计算_?初沉池、二沉池的作用与区别-亨孚科技