如何在MySQL数据库中定义与使用游标

本篇文章给大家分享的是有关如何在MySQL数据库中定义与使用游标,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创建游标

首先在MySql中创建一张数据表:

CREATE TABLE IF NOT EXISTS `store` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(20) NOT NULL,  `count` int(11) NOT NULL DEFAULT '1',  PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;   INSERT INTO `store` (`id`, `name`, `count`) VALUES (1, 'android', 15), (2, 'iphone', 14), (3, 'iphone', 20), (4, 'android', 5), (5, 'android', 13), (6, 'iphone', 13);

我们现在要用存储过程做一个功能,统计iphone的总库存是多少,并把总数输出到控制台。

--在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();

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。