标签:
游标
mysql> delimiter // mysql> create procedure duhuo() -> begin -> declare ordnums cursor -> for -> select order_num from orders; -> end -> //
OPEN ordnums;
close ordnums;
mysql> delimiter // mysql> create procedure duhuo1() -> begin -> declare dudu cursor -> for -> select order_num from orders; -> open dudu; -> close dudu; -> end -> //
mysql> delimiter // mysql> create procedure demo0() -> begin -> declare tx int; -> declare du1 cursor -> for -> select order_num from orders; -> open du1; -> fetch du1 into tx; -> close du1; -> end -> // Query OK, 0 rows affected (0.02 sec)
mysql> create procedure demo0() -> begin -> delcare done boolean default 0; -> declare du int; -> declare ordernu cursor -> for -> select order_num from orders; -> declare continue handler for sqlstate ‘02000‘ set done = 1; -> open ordernu; -> repeat -> fetch ordernu into du; -> until done end repeat; -> close ordernu; -> end//
declare continue handler for sqlstate ‘02000‘ set done = 1;
mysql> delimiter // mysql> create procedure liwei() -> begin -> declare done boolean default 0; -> declare li int; -> declare wei decimal(8,2); -> declare numb cursor -> for -> select order_num from orders; -> declare continue handler for sqlstate ‘02000‘ set done=1; -> create table if not exists ordertotals -> (order_num int,total decimal(8,2)); -> open numb; -> repeat -> fetch ordernumbers into li; -> call ordertotals(tx,1,t); -> insert into ordertotals(order_num, total) -> values(li,wei); -> until done end repeat; -> close numd; -> end -> //
select * from ordertotals;
+----------------+--------------+ | order_num | total | +----------------+--------------+ | 20005 | 158.56 | | 20006 | 25.78 | | 20007 | 1068.00 | +----------------+--------------+
标签:
原文地址:http://www.cnblogs.com/duhuo/p/4651107.html