设为首页 加入收藏

TOP

mysql学习记录(十)--存储过程(五)
2015-11-21 01:38:17 来源: 作者: 【 】 浏览:3
Tags:mysql 学习 记录 存储 过程
@x1 | @x2 | +------+-------+ | 5 | 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.02 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 + 6; -> else -> set @x2 = @x2+ d_amount + 5; -> 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(); +------+-------+ | @x1 | @x2 | +------+-------+ | 15 | 30005 | +------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER $$ mysql> mysql> CREATE PROCEDURE addNum() -> BEGIN -> DECLARE x INT; -> SET x = 0; -> for_loop : LOOP -> SET x = x + 1; -> IF x > 30 THEN -> LEAVE for_loop; -> END IF; -> IF mod(x,2) = 0 then -> select "num:",x; -> ITERATE for_loop; -> END IF; -> END LOOP; -> select "count:",x; -> END $$ Query OK, 0 rows affected (0.01 sec) mysql> call addNum(); -> $$ +------+------+ | num: | x | +------+------+ | num: | 2 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 4 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 6 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 8 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 10 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 12 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 14 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 16 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 18 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 20 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 22 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 24 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 26 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 28 | +------+------+ 1 row in set (0.00 sec) +------+------+ | num: | x | +------+------+ | num: | 30 | +------+------+ 1 row in set (0.00 sec) +--------+------+ | count: | x | +--------+------+ | count: | 31 | +--------+------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> delimiter $$ mysql> create procedure repeatPractise() -> begin -> set @v = 0 ; -> REPEAT -> set @v = @v+ 1; -> UNTIL @v >=5 -> END REPEAT; -> END -> $$ Query OK, 0 rows affected (0.0
首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇卸载MySQL5.0 下一篇不同场景下 MySQL 的迁移方案

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: