本文章来给各位同学详细介绍关于在MySQL存储过程游标使用实例,包括简单游标使用与游标循环跳出等方法,希望此教程对各位同学有所帮助.
例1,一个简单存储过程游标实例,代码如下:
- DELIMITER $$
- DROP PROCEDURE IF EXISTS getUserInfo $$
- CREATE PROCEDURE getUserInfo(in date_day datetime)
-
-
-
-
-
- BEGIN
- declare _userName varchar(12);
- declare _chinese int ;
- declare _math int ;
- declare done int;
-
- DECLARE rs_cursor CURSOR FOR SELECT username,chinese,math from userInfo where datediff(createDate, date_day)=0;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-
- if date_day is null then
- set date_day = date_add(now(),interval -1 day);
- end if;
- open rs_cursor;
- cursor_loop:loop
- FETCH rs_cursor into _userName, _chinese, _math;
-
- if done=1 then
- leave cursor_loop;
- end if;
-
- update infoSum set total=_chinese+_math where UserName=_userName;
- end loop cursor_loop;
- close rs_cursor;
-
- END$$
- DELIMITER ;
例2,存储过程游标循环跳出现
在MySQL的存储过程中,游标操作时,需要执行一个conitnue的操作.众所周知,MySQL中的游标循环操作常用的有三种,LOOP,REPEAT,WHILE.三种循环,方式大同小异.以前从没用过,所以记下来,方便以后查阅.
1.REPEAT,代码如下:
- REPEAT
- Statements;
- UNTIL expression
- END REPEAT
- demo
- DECLARE num INT;
- DECLARE my_string VARCHAR(255);
- REPEAT
- SET my_string =CONCAT(my_string,num,',');
- SET num = num +1;
- UNTIL num <5
- END REPEAT;
2.WHILE,代码如下:
- WHILE expression DO
- Statements;
- END WHILE
- demo
- DECLARE num INT;
- DECLARE my_string VARCHAR(255);
- SET num =1;
- SET str ='';
- WHILE num < span>10DO
- SET my_string =CONCAT(my_string,num,',');
- SET num = num +1;
- END WHILE;
3.LOOP(这里面有非常重要的ITERATE,LEAVE),代码如下:
- DECLARE num INT;
- DECLARE str VARCHAR(255);
- SET num =1;
- SET my_string ='';
- loop_label: LOOP
- IF num <10THEN
- LEAVE loop_label;
- ENDIF;
- SET num = num +1;
- IF(num mod3)THEN
- ITERATE loop_label;
- ELSE
- SET my_string =CONCAT(my_string,num,',');
- ENDIF;
- END LOOP;
PS:可以这样理解ITERATE就是我们程序中常用的contiune,而ITERATE就是break.当然在MySQL存储过程,需要循环结构有个名称,其他都是一样的.
例3,mysql 存储过程中使用多游标,先创建一张表,插入一些测试数据,代码如下:
- DROP TABLE IF EXISTS netingcn_proc_test;
- CREATE TABLE `netingcn_proc_test` (
- `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
- `name` VARCHAR(20),
- `password` VARCHAR(20),
- PRIMARY KEY (`id`)
- )ENGINE=InnoDB;
- insert into netingcn_proc_test(name, password) values
- ('procedure1', 'pass1'),
- ('procedure2', 'pass2'),
- ('procedure3', 'pass3'),
- ('procedure4', 'pass4');下面就是一个简单存储过程的例子:
- drop procedure IF EXISTS test_proc;
- delimiter //
- create procedure test_proc()
- begin
-
- DECLARE done INT DEFAULT 0;
-
-
- DECLARE tname varchar(50) DEFAULT NULL;
- DECLARE tpass varchar(50) DEFAULT NULL;
-
- DECLARE cur CURSOR FOR
- select name, password from netingcn_proc_test;
-
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-
- open cur;
-
- REPEAT
-
- FETCH cur INTO tname, tpass;
- if not done then
-
- select tname, tpass;
- end if;
- UNTIL done END REPEAT;
- CLOSE cur;
- end
- //
- delimiter ;
-
- call test_proc();
需要注意的是变量的声明、游标的声明和HANDLER声明的顺序不能搞错,必须是先声明变量,再申明游标,最后声明HANDLER。上述存储过程的例子中只使用了一个游标,那么如果要使用两个或者更多游标怎么办,其实很简单,可以这么说,一个怎么用两个就是怎么用的,例子如下:
- drop procedure IF EXISTS test_proc_1;
- delimiter //
- create procedure test_proc_1()
- begin
- DECLARE done INT DEFAULT 0;
- DECLARE tid int(11) DEFAULT 0;
- DECLARE tname varchar(50) DEFAULT NULL;
- DECLARE tpass varchar(50) DEFAULT NULL;
- DECLARE cur_1 CURSOR FOR
- select name, password from netingcn_proc_test;
- DECLARE cur_2 CURSOR FOR
- select id, name from netingcn_proc_test;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- open cur_1;
- REPEAT
- FETCH cur_1 INTO tname, tpass;
- if not done then
- select tname, tpass;
- end if;
- UNTIL done END REPEAT;
- CLOSE cur_1;
-
- set done = 0;
- open cur_2;
- REPEAT
- FETCH cur_2 INTO tid, tname;
- if not done then
- select tid, tname;
- end if;
- UNTIL done END REPEAT;
- CLOSE cur_2;
- end
- //
- delimiter ;
- call test_proc_1();
上述代码和第一个例子中基本一样,就是多了一个游标声明和遍历游标,这里需要注意的是,在遍历第二个游标前使用了set done = 0,因为当第一个游标遍历玩后其值被handler设置为1了,如果不用set把它设置为 0,那么第二个游标就不会遍历了,当然好习惯是在每个打开游标的操作前都用该语句,确保游标能真正遍历,当然还可以使用begin语句块嵌套的方式来处理多个游标,例如:
- drop procedure IF EXISTS test_proc_2;
- delimiter //
- create procedure test_proc_2()
- begin
- DECLARE done INT DEFAULT 0;
- DECLARE tname varchar(50) DEFAULT NULL;
- DECLARE tpass varchar(50) DEFAULT NULL;
- DECLARE cur_1 CURSOR FOR
- select name, password from netingcn_proc_test;
- DECLARE cur_2 CURSOR FOR
- select id, name from netingcn_proc_test;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- open cur_1;
- REPEAT
- FETCH cur_1 INTO tname, tpass;
- if not done then
- select tname, tpass;
- end if;
- UNTIL done END REPEAT;
- CLOSE cur_1;
- begin
- DECLARE done INT DEFAULT 0;
- DECLARE tid int(11) DEFAULT 0;
- DECLARE tname varchar(50) DEFAULT NULL;
- DECLARE cur_2 CURSOR FOR
- select id, name from netingcn_proc_test;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- open cur_2; //phpfensi.com
- REPEAT
- FETCH cur_2 INTO tid, tname;
- if not done then
- select tid, tname;
- end if;
- UNTIL done END REPEAT;
- CLOSE cur_2;
- end;
- end
- //
- delimiter ;
- call test_proc_2();
|