創(chuàng)建游標(biāo) declare cursor_name cursor for select...
打開和關(guān)閉游標(biāo) open/close cursor_name;
使用游標(biāo)數(shù)據(jù)
利用fetch語句分別訪問它的每一行,fetch語句指定檢索什么數(shù)據(jù),檢索出來的數(shù)據(jù)存儲在什么地方,它還向前移動游標(biāo)中的內(nèi)部指針,使下一條fetch語句檢索下一行。 fetch cursor_name into tmp;
游標(biāo)使用演示
create procedure processorders()
begin
declare done boolean default 0;
declare o int;
declare t decimal(8,2);
declare ordernumbers cursor
for
select order_num from orders;
declare continue handler for sqlstate '02000' set done=1;//'02000'指未找到
create table if not exists ordertotals(order_num int ,total decimal(8,2));
//打開游標(biāo)
open ordernumbers;
repeat
fetch ordernumbers into o;
call ordertotal(o,1,t);
insert into ordertotals(order_num,total) values(o,t);
until done end repeat;
close ordernumbers;
end