22 使用游标

22.1 游标

  • MySQL检索操作返回一组称为结果集的行。这组返回的行都是与SQL语句相匹配的行(零行或多行)。使用简单的select语句,例如,没有办法得到第一行、下一行或前10行,也不存在每次一行地处理所有行的简单方法。
  • 有时,需要在检索出来的行中前进或后退一行或多行。这就是使用游标的原因。游标是一个存储在MySQL服务器上的数 据查询,它不是一条select语句,而是被该语句检索出来的结果集。在存储了游标之后,应用程序可以根据需要滚动或浏览其中的数据。
  • 游标主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或做出更改。
  • 只能用于存储过程 MySQL游标只能用于存储过程(和函数)。

22.2 使用游标

  • 使用游标涉及几个明确的步骤。
    • 在能够使用游标前,必须声明它。这个过程实际上没有检索数据,它只是定义要使用的select语句。
    • 一旦声明后,必须打开游标以供使用。这个过程用前面定义的select语句把数据实际检索出来。
    • 对于填有数据的游标,根据需要取出各行。
    • 在结束游标使用时,必须关闭游标。
  • 在声明游标后,可根据需要频繁地打开和关闭游标。在游标打开后,可根据需要频繁地执行取操作。

22.2.1 创建游标

  • 游标用DECLARE语句创建。DECLARE命名游标,并定义相应的SELECT语句,根据需要带WHERE和其他子句。例如,下面的语句定义了名为ordernumbers的游标,使用了可以检索所有订单的SELECT语句。
create procedure processorders()
begin
    declare ordernumbers cursor
    for
    select order_num from orders;
end;
  • 这个存储过程并没有做很多事情,declare语句用来定义和命名游标,这里为ordernumbers。存储过程处理完成 后,游标就消失。
  • 在定义游标之后,可以打开它

22.2.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 orders;
    
    -- open the cursor
    open ordernumbers;
    
    -- close the cursor
    close ordernumber;
end;
  • 这个存储过程声明,打开和关闭一个游标。但对检索出的数据什么也没做。

22.2.3 使用游标数据

  • 在一个游标被打开后,可以使用fetch语句分别访问它的每一行。fetch指定检索什么数据(所需的列),检索出来的数据存储在什么地方。它还向前移动游标中的内部行指针,使下一条fetch语句检索下一行。
  • 第一个例子从游标中检索单个行:
create procedure processorders()
begin
    -- declare local variables
    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 variables
    declare done boolean default 0;
    declare o int;
    
    -- declare the cursor
    declare ordernumbers cursor
    for 
    select order_num from orders;
    
    -- declare continue handler
    declare continue handler for sqlstate '02000' set done=1;
    
    -- open the cursor
    open ordernumbers;
    
    -- Loop through all rows
    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为真。为使它起作用,用一个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语句之后,循环结束之前)
  • 重复或循环? 除这里使用的repeat语句外,MySQL还支持循环语句,它 可用来重复执行代码,直到使用LEAVE语句手动退出为止。通常REPEAT语句的语法使它更适合于对游标进行循环。
  • 为了把这些内容组织起来,下面给出我们游标存储过程样例的更进一步修改的版本,这次对取出的数据进行某种实际的处理:
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 number
        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;
  • 此存储过程还在运行中创建了一个新表,名为ordertotals。这个表将保存存储过程生成的结果。fetch像以前一样取每个order_num,然后用call执行另一个存储过程来计算每个订单的带税的合计。最后,用insert保存每个订单的订单号和合计。
  • 此存储过程不返回数据,但它能够创建和填充另一个表,可以用一条简单的select语句查看该表:
select * 
from ordertotal;
  • 这样,我们就得到了存储过程、游标、逐行处理以及存储过程调用其他存储过程的一个完整的工作样例。
原文地址:https://www.cnblogs.com/sanjun/p/8289962.html