MySQL检所操作返回一组称为结果集的行,游标是一个存储在MySQL服务器上的数据库查询,它不是一条select语句,而是被该语句所检索出来的结果集。只能用于存出过程。
DECLARE cursor_avgScore CURSOR
OPEN cursor_avgScore ;
CLOSE cursor_avgScore ;
close使用游标所使用的内部内存和资源,因此游标不使用时要关闭。
一个关闭后的游标不打开不能再次使用,但是一个声明后的游标不需要再次声明,用open打开就可以。
有一个student2表,表中的结构和数据如下: 现在我们需要将student2表中所有学生的的平均成绩(mathScore+englishScore的一半)合成一行,用逗号’,’隔开。
DELIMITER $$CREATE PROCEDURE `test`.`procedure_student2`() BEGIN -- declare some variable,必须在声明游标和句柄之前,而声明句柄必须在声明游标之后。 DECLARE val DOUBLE DEFAULT 0; DECLARE tempRes VARCHAR(10) DEFAULT ‘‘; DECLARE res VARCHAR(100) DEFAULT ‘‘ ; -- declare a cursor DECLARE cursor_avgScore CURSOR FOR SELECT (mathScore+englishScore)/2 AS student_avgScore FROM student2; -- declare a continue handler ,use finish while loop DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET val= -1.0 ; -- open cursor OPEN cursor_avgScore ; FETCH cursor_avgScore INTO val; -- fetch cursor WHILE val!=-1 DO SET tempRes=CONCAT(val,‘, ‘); SET res=CONCAT(res,tempRes); FETCH cursor_avgScore INTO val; END WHILE; -- close cursor CLOSE cursor_avgScore ; -- 显示结果 SELECT res; END$$DELIMITER ;
在MySQL中我们还可以用repeat来做
DELIMITER $$CREATE PROCEDURE `test`.`procedure_student_v1`() BEGIN -- declare some variable,必须在声明游标和句柄之前,而声明句柄必须在声明游标之后。 DECLARE val DOUBLE DEFAULT 0; DECLARE tempRes VARCHAR(10) DEFAULT ‘‘; DECLARE res VARCHAR(100) DEFAULT ‘‘ ; -- declare a cursor DECLARE cursor_avgScore CURSOR FOR SELECT (mathScore+englishScore)/2 AS student_avgScore FROM student2; -- declare a continue handler ,use finish while loop DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET val= -1.0 ; -- open cursor OPEN cursor_avgScore ; -- fetch cursor REPEAT FETCH cursor_avgScore INTO val; IF val!=-1 THEN SET tempRes=CONCAT(val,‘, ‘); SET res=CONCAT(res,tempRes); END IF; UNTIL val=-1 END REPEAT; -- 居然MySQL中 用 val=-1 来结束循环,原以为应该和java、c类似,用val==-1来结束。 -- close cursor CLOSE cursor_avgScore ; -- 显示结果 SELECT res; END$$DELIMITER ;
MySQL中while,loop,repeat循环
-- MySQL中的三中循环 while 、 loop 、repeat 求 1-n 的和-- 第一种 while 循环 -- 求 1-n 的和/* while循环语法:while 条件 DO 循环体;end while;*/-- 实例:create procedure sum1(a int) begin declare sum int default 0; -- default 是指定该变量的默认值 declare i int default 1;while i<=a DO -- 循环开始 set sum=sum+i; set i=i+1;end while; -- 循环结束select sum; -- 输出结果end-- 执行存储过程call sum1(100);-- 删除存储过程drop procedure if exists sum1-- 第二种 loop 循环/*loop 循环语法:loop_name:loop if 条件 THEN -- 满足条件时离开循环 leave loop_name; -- 和 break 差不多都是结束训话 end if;end loop;*/-- 实例:create procedure sum2(a int)begin declare sum int default 0; declare i int default 1; loop_name:loop -- 循环开始 if i>a then leave loop_name; -- 判断条件成立则结束循环 好比java中的 boeak end if; set sum=sum+i; set i=i+1; end loop; -- 循环结束 select sum; -- 输出结果end-- 执行存储过程call sum2(100);-- 删除存储过程drop procedure if exists sum2-- 第三种 repeat 循环/*repeat 循环语法repeat 循环体until 条件 end repeat;*/-- 实例;create procedure sum3(a int)begin declare sum int default 0; declare i int default 1; repeat -- 循环开始 set sum=sum+i; set i=i+1; until i>a end repeat; -- 循环结束 select sum; -- 输出结果end-- 执行存储过程call sum3(100);-- 删除存储过程drop procedure if exists sum3