使用游标数据
在一个游标被打开后,可以使用 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