MySQL必知必会——第24章 使用游标 读书笔记

本章讲授了什么是游标,如何使用游标。
1 什么是游标
游标只用于MySQL5.0及以后的版本。
MySQL检索操作返回一组称为结果集的行。这组返回的行都是与SQL语句相匹配的行(零行或者多行)。
使用简单的select语句没有办法得到第一行、下一行或前10行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们)。
有时,需要在检索出来的行中前进或后退一行或多行。这就是使用游标的原因。
游标是一个存储在MySQL服务器上的数据库查询,它不是一条select语句,而是被该语句检索出来的结果集
在存储了游标之后,应用程序可以根据需要滚动或浏览其中的数据。
游标主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或更改。
MySQL游标只能用于存储过程。
2 使用游标
步骤:①在能够使用游标前,必须声明它。这个过程实际上没有检索数据,它只是定义要使用的select语句。
②一旦声明后,必须打开游标以供使用。这个过程用前面定义的select语句把数据实际检索出来。
③对于填有数据的游标,根据需要取出(检索)各行。
④在结束游标使用后,必须关闭游标。
在声明游标后,可根据需要频繁地打开和关闭游标。在游标打开后,可根据需要频繁地执行取操作。
(1)创建游标
游标用declare语句创建。declare命名游标,并定义相应的select语句,根据需要带where和其他子句。
例如,下面语句定义了名为ordernumbers的游标,使用了可以检索所有订单的select语句。

create procedure processorders()
begin
  declare ordernumbers cursor
  for
  select order_num from orders;
end;

这个存储过程并没有做很多事情,declare语句用来定义和命名游标,这里为ordernumbers。存储过程完成后,游标就消失(游标仅限于存储过程)。
在定义游标后,可以打开它。
(2)打开和关闭游标
游标用open cursor语句来打开:

open ordernumbers;

处理open语句时执行查询,存储检索出的数据以供浏览和滚动。
游标处理完成后,使用如下语句关闭游标:

close ordernumbers;

close释放游标使用的所有内部内存和资源,因此每个游标不再使用时都应该关闭。
在一个游标关闭后,如果没有重新打开, 则不能使用它。但是,使用声明过的游标不需要再次声明,用open语句打开它就可以了。
隐含关闭:如果你不明确关闭游标,MySQL将会在到达end语句时自动关闭它。
下面是前面例子的修改版本:

create procedure processorders()
begin
  --declare the cursor
  declare ordernumbers cursor
  for 
  select order_num from orders;
  --open the cursor
  open ordernumbers;
  --close the cursor
  close ordernumbers;
end;

这个存储过程声明、打开和关闭一个游标。但对检索出的数据什么也没做。
(3)使用游标数据
在一个游标被打开时,可以使用fetch语句分别访问它的每一行。fetch指定检索什么数据(所需的列),检索出来的数据存储在什么地方。它还向前移动游标中的内部行指针,使下一条fetch语句检索下一行(不重复读取同一行)。
第一个例子从游标中检索单个行(第一行)。

create procedure processorders()
begin
--declare local variable
declare  o int;
--declare the cursor
declare ordernumbers cursor
for
select order_num from orders;

--open the cursor
open ordernumbers;
--get order number
fetch ordernumbers into o;
--close the cursor
close ordernumbers;
end;

其中fetch用来检索当前行的order_num列(将自动从第一行开始)到一个名为o的局部声明的变量中。对检索出的数据不做任何处理。
在下一个例子中,循环检索数据,从最后一行到第一行:

create procedure processorders()
begin
--declare local variable 
declare done boolean default 0;
declare o int;
--declare the cursor
declare ordernumbers cursor
for 
select order_num from orders;
--open the cursor
open ordernumbers;
--loop through all done
repeat
--get order number
fetch ordernumbers into o;
--end of loop
until done end repeat;
--close the cursor
close ordernumbers;
end;

与前面一个例子一样,这个例子使用fetch检索当前order_num到声明为o的变量中。但与前一个例子不同的是,这个fetch是在repeat内,因为它反复执行直到done为真(由until done end repeat;规定)。为了使它起作用,用一个default 0来定义变量done。那么done怎样才能在结束时被设置为真呢?答案是用以下语句:

declare continue handler for SQLSTATE '02000' SET done=1;

这个语句定义了一个continue handler,它是在条件出现时被执行的代码。当SQLSTATE‘02000’出现时,set done=1。SQLSTATE’02000’是一个未找到条件,当REPEAT由于没有更多的行供循环而不能继续时,出现这个条件。
declare语句的次序:declare语句的发布存在特定的次序。用declare定义的局部变量必须在定义任意游标或句柄之前定义,而句柄必须在游标之后定义。不遵守此顺序将产生错误消息。
如果调用这个存储过程,它将定义几个变量和一个continue handler,定义并打开一个游标,重复读取所有行,然后关闭游标。
如果一切正常,你可以在循环内放入任意需要的处理(在fetch语句之后,循环结束之前)
为了把这些内容组织起来,下面给出我们的游标存储过程样例的更进一步修改的版本,这次对取出的数据进行某些实际的处理:

create procedure processorders()
begin
--declare local variables
declare done boolean default 0;
declare o int;
declare t decimal(8,2);
--declare the cursor
declare ordernumbers cursor
for
select order_num from orders;
--declare continue handler
declare continue handler for sqlstate '02000' set done=1;
--create a table to store the results
create table if not  exists ordertotals(order_num int,total decimal(8,2));
--open the cursor
open ordernumbers;
--loop through all rows
repeat
--get order numbers
fetch ordernumbers into o;
--get the total for this order
call ordertotal(o,1,t);
--insert order and total into ordertotals
insert into ordertotals(order_num,total) values(o,t);
--end of loop
until done end repeat;
--close the cursor
close ordernumbers;
end;

在这个例子中,我们增加了另一个名为t的变量(存储每个订单的合计)。
此存储过程还在运行中创建了一个新表ordertotals。这个表将保存存储过程中生成的结果。
fetch像以前一样取每一个order_num,然后用call执行另一个存储过程(前一章中创建)来计算每个订单的带税的合计(结果存储到t)。
最后用insert保存每个订单的订单号和合计。
此存储过程不返回数据,但它能创建和填充另一个表,可以用select语句查看表的内容:

select * from ordertotals;

这样我们就得到了存储过程
游标、逐行处理以及存储过程调用其他存储过程的一个完整的工作样例。

小结
本章介绍了什么是游标、为什么要使用游标,说明了基本游标使用的例子,讲解了对游标结果进行循环以及逐行处理的技术。

发布了90 篇原创文章 · 获赞 8 · 访问量 8227

猜你喜欢

转载自blog.csdn.net/weixin_43854189/article/details/104049951
今日推荐