mysql学习记录(十)--存储过程(四)

2015-11-21 01:38:17 · 作者: · 浏览: 15
er $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> declare tmp_name varchar(30) default ""; -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> call payment_stat(); -> $$ +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.01 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id + 1; -> else -> set @x2 = @x2+ d_amount ; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql>
drop procedure payment_stat; Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.00 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ | @x1 | @x2 | +------+-------+ | 3 | 30000 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure payment_stat() -> begin -> DECLARE i_staff_id int; -> DECLARE d_amount int; -> -> DECLARE cur_payment cursor for select staff_id,amount from payment; -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment; -> -> set @x1 = 0 ; -> set @x2 = 0 ; -> -> open cur_payment; -> fetch cur_payment into i_staff_id,d_amount; -> while(i_staff_id <=3 ) -> do -> if i_staff_id < 3 then -> set @x1 = @x1+ i_staff_id + 1; -> else -> set @x2 = @x2+ d_amount; -> end if; -> fetch cur_payment into i_staff_id,d_amount; -> end while; -> close cur_payment; -> -> select @x1,@x2; -> end; -> $$ Query OK, 0 rows affected (0.01 sec) mysql> delimiter ; mysql> call payment_stat(); +------+-------+ |