设为首页 加入收藏

TOP

mysql学习记录(十)--存储过程(四)
2015-11-21 01:38:17 来源: 作者: 【 】 浏览:1
Tags:mysql 学习 记录 存储 过程
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(); +------+-------+ |
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇卸载MySQL5.0 下一篇不同场景下 MySQL 的迁移方案

评论

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