mysql>delimiter //
mysql>
mysql>CREATE PROCEDURE curdemo(IN V_GROUP INT)
->BEGIN
-> DECLARE done INT DEFAULT 0
-> DECLARE a INT
-> DECLARE b CHAR(16)
->
-> DECLARE cur1 CURSOR FOR SELECT id,name FROM student where classid=V_GROUP
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1
->
-> OPEN cur1
->
-> REPEAT
->FETCH cur1 INTO a, b
->select a,b
-> UNTIL done END REPEAT
->
-> CLOSE cur1
->END
->
->
->//
Query OK, 0 rows affected (0.20 sec)
mysql>
mysql>delimiter
mysql>
mysql> SELECT id,name,classid FROM student
+----------+--------+---------+
| id | name | classid |
+----------+--------+---------+
| 91010101 | 丁乙甲 | 10101 |
| 91010102 | 索春龙 | 10101 |
| 91010103 | 刘莉莉 | 10101 |
| 91010104 | 隋鑫 | 10101 |
| 91010105 | 长命 | 10101 |
| 91010201 | 河江山 | 10102 |
| 91010202 | 祝立业 | 10102 |
| 91010203 | 姚桂娇 | 10102 |
| 91010204 | 张冬梅 | 10102 |
| 91020101 | 蒋旭 | 10201 |
| 91020102 | 邱介臣 | 10201 |
| 91020103 | 王晓燕 | 10201 |
| 91020104 | 夏丽佳 | 10201 |
| 91020201 | 熊建业 | 10202 |
| 91020202 | 丛俊江 | 10202 |
| 91020203 | 周静燕 | 10202 |
| 91020204 | 黄爽 | 10202 |
| 91020301 | 魏国兴 | 10203 |
| 91020302 | 蒋洪涛 | 10203 |
| 91020303 | 倪春红 | 10203 |
| 91030101 | 张文天 | 10301 |
| 91030102 | 姜洋 | 10301 |
| 91030103 | 赵聪 | 10301 |
| 91030201 | 王飞 | 10302 |
+----------+--------+---------+
24 rows in set (0.00 sec)
mysql>
mysql>CALL curdemo(10101)
+----------+--------+
| a| b |
+----------+--------+
| 91010101 | 丁乙甲 |
+----------+--------+
1 row in set (0.00 sec)
+----------+--------+
| a| b |
+----------+--------+
| 91010102 | 索春龙 |
+----------+--------+
1 row in set (0.00 sec)
+----------+--------+
| a| b |
+----------+--------+
| 91010103 | 刘莉莉 |
+----------+--------+
1 row in set (0.00 sec)
+----------+------+
| a| b|
+----------+------+
| 91010104 | 隋鑫 |
+----------+------+
1 row in set (0.00 sec)
+----------+------+
| a| b|
+----------+------+
| 91010105 | 长命 |
+----------+------+
1 row in set (0.00 sec)
+----------+------+
| a| b|
+----------+------+
| 91010105 | 长命 |
+----------+------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
先声明一个游标,语法:DECLARE v_1 VARCHAR(16)
DECLARE v_2 VARCHAR(16)
DECLARE c_XXX CURSOR FOR
SELECT c_1, c_2
FROM t_1
同时声明一个布尔型的变量FOUND,当循环的条件不成立时,结束循环用的。
DECLARE FOUND BOOLEAN DEFAULT TRUE
DECLARE CONTINUE HANDLER FOR NOT FOUND SET FOUND = FALSE
在使用游标的时候,要先打开游标,语法:OPEN c_XXX
使用的时候,要先打开游标,取出第一条数据,语法: FETCH c_XXX INTO v_1, v_2
然后才开始执行循环,语法:WHILE FOUND DO
执行需要执行的语句;
取下一条数据放到当前游标中,FETCH c_XXX INTO v_1, v_2
结束循环,语法:END WHILE
结束游标,语法:CLOSE c_cargo。有什么问题我们再沟通啊。不知道你具体要问题的是什么。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)