第24章 使用游标

  1. 游标
  2. 使用游标
  3. 创建游标
  4. 打开和关闭游标
  5. 使用游标数据

  

一、游标

  由前几章可知,MySQL检索操作返回一组称为结果集的行。这组返回的行都是与SQL语句相匹配的行(零行或多行)。使用简单的select语句,例如,没有办法得到第一行、下一行或前10行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们)。

  有时,需要在检索出来的行中前进或后退一行或多行。这就是使用游标的原因。游标是一个存储在MySQL服务器上的数据库查询,它不是一条select语句,而是被该语句检索出来的结果集。在存储了游标之后,应用程序可以根据需要滚动或浏览其中的数据。

  游标主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或做出更改。

只能用于存储过程不像多数DBMS,MySQL游标只能用于存储过程(和函数)。

  

二、使用游标

  使用游标涉及几个明确的步骤。

  ※ 在能够使用游标前,必须声明(定义)它。这个过程实际上没有检索数据,它只是定义要使用的select语句。  ※ 一旦声明后,必须打开游标以供使用。这个过程用前面定义的select语句把数据实际检索出来。  ※ 对于填有数据的游标,根据需要取出(检索)各行。  ※ 在结束游标使用时,必须关闭游标。

  在声明游标后,可根据需要频繁地打开和关闭游标。在游标打开后,可根据需要频繁地执行取操作。

1、创建游标

  游标用declare语句创建(参见第23章)。delcare命名游标,并定义相应的select语句,根据需要带where和其他子句。例如,下面的语句定义了名为ordernumbers的游标,使用了可以检索所有订单的select语句。

  1. create procedur processorders()
  2. begin
  3. declare ordernumbers cursor
  4. for
  5. select order_num from orders;
  6. end;

  这个存储过程并没有做很多事情,declare语句用来定义和命名游标,这里为ordernumbers。存储过程处理完成后,游标就消失(因为它局限于存储过程)。

  在定义游标之后,可以打开它。

2、打开和关闭游标

  游标用open cursor语句来打开:

  1. open ordernumbers;

  在处理open语句时执行查询,存储检索出的数据以供浏览和滚动。

  游标处理完成后,应当使用如下语句关闭游标:

  1. close ordernumbers;

  close释放游标使用的所有内部内存资源,因此在每个游标不再需要时都应该关闭。

  在一个游标关闭后,如果没有重新打开,则不能使用它。但是,使用声明过的游标不需要再次声明,用open语句打开它就可以了。

隐含关闭如果你不明确关闭游标,MySQL将会在到达end语句时自动关闭它。

  下面是前面例子的修改版本:

  1. create procedure processorders()
  2. begin
  3. -- Declare the cursor
  4. declare ordernumbers cursor
  5. for
  6. select order_num from orders;
  7. -- Open the cursor
  8. open ordernumbers;
  9. -- Close the cursor
  10. close ordernumbers;
  11. end;

  这个存储过程声明、打开和关闭一个游标。但对检索出的数据什么也没做。

3、使用游标数据

  在一个游标被打开后,可以使用fetch语句分别访问它的每一行。fetch指定检索什么数据(所需的列),检索出来的数据存储在什么地方。它还向前移动游标中的内部行指针,使下一条fetch语句检索下一行(不重复读取同一行)。

  第一个例子从游标中检索单个行(第一行):

  1. create procedure processorders()
  2. begin
  3. -- Declare local variables
  4. declare o int;
  5. -- Declare the cursor
  6. declare ordernumbers cursor
  7. for
  8. select order_num from orders;
  9. -- Open the cursor
  10. open ordernumbers;
  11. -- Get order number
  12. fetch ordernumbers into o;
  13. -- Close the cursor
  14. close ordernumbers;
  15. end;

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

  在下一个例子中,循环检索数据,从第一行到最后一行:

  1. create procedure processorders()
  2. begin
  3. -- Declare local variables
  4. declare done boolean default 0;
  5. declare o int;
  6. -- Declare the cursor
  7. declare ordernumbers cursor
  8. for
  9. select order_num from orders;
  10. -- Declare continue handler
  11. delcare continue handler for sqlstate '02000' set done=1;
  12. -- Open the cursor
  13. open ordernumbers;
  14. -- Loop through all rows
  15. repeat
  16. -- Get order number
  17. fetch ordernumbers into o;
  18. -- End of loop
  19. until done end repeat;
  20. -- Close the cursor
  21. close ordernumbers;
  22. end;

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

  1. declare continue handler for sqlstate '02000' done=1;

  这条语句定义了一个continue handler,它是在条件出现时被执行的代码。这里,它指出当sqlstate '02000'出现时,set done=1sqlstate '02000'是一个未找到条件,当repeat由于没有更多的行供循环而不能继续时,出现这个条件。

declare语句的次序declare语句的发布存在特定的次序。用declare语句定义的局部变量必须在定义任意游标或句柄之前定义,而句柄必须在游标之后定义。不遵守此顺序将产生错误消息。

  如果调用这个存储过程,它将定义几个变量和一个continue handler,定义并打开一个游标,重复读取所有行,然后关闭游标。

  如果一起正常,你可以在循环内放入任意需要的处理(在fetch语句之后,循环结束之前)。

重复或循环?除这里使用的repeat语句外,MySQL还支持循环语句,它可用来重复执行代码,直到使用leave语句手动退出为止。通常repeat语句的语法使它更适合于对游标进行循环。

  为了把这些内容组织起来,下面给出我们的游标存储过程样例的更进一步修改的版本,这次对取出的数据进行某种实际的处理:

  1. create procedure processorders()
  2. begin
  3. -- Declare local variables
  4. declare done boolean default 0;
  5. declare o int;
  6. declare t decimal(8,2);
  7. -- Declare the cursor
  8. declare ordernumbers cursor;
  9. -- Declare continue handler
  10. declare continue handler for sqlstate '02000' set done=1;
  11. -- Create a table to store the results
  12. create table if not exists ordertotals
  13. (order_num int, total decimal(8,2));
  14. -- Open the cursor
  15. open ordernumbers;
  16. -- Loop through all rows
  17. repeat
  18. -- Get order number
  19. fetch ordernumbers into o;
  20. -- Get the total for this order
  21. call ordertotal(o, 1, t);
  22. -- Insert order and total into ordertotals
  23. insert into ordertotals(order_num, total)
  24. values(o, t);
  25. -- End of loop
  26. until done end repeat;
  27. -- Close the cursor
  28. close ordernumbers;
  29. end;

  在这个例子中,我们增加了另一个名为t的变量(存储每个订单的合计)。此存储过程还在运行中创建了一个新表(如果它不存在的话),名为ordertotals。这个表将保存存储过程生成的结果。fetch像以前一样取每个order_num,然后用call执行另一个存储过程(我们在前一章中创建)来计算每个订单的带税的合计(结果存储到t)。最后,用insert保存每个订单的订单号和合计。

  此存储过程不返回数据,但它能够创建和填充另一个表,可以用一条简单的select语句查看该表:

  1. select *
  2. from ordertotals;

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

  

?