|
@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 |