(编辑:jimmy 日期: 2024/12/26 浏览:2)
mysql声明游标的方法:
1、声明变量和游标
declare result varchar(3000); declare flag1 varchar(100); DECLARE done INT DEFAULT FALSE; declare cur1 CURSOR FOR SELECT flag from bmlx;
2、变量赋值
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; set result = '';
3、打开游标和循环
open cur1; read_loop: LOOP
-- 给游标赋值
FETCH cur1 INTO flag1;
-- 跳出循环
IF done THEN LEAVE read_loop; END IF;
-- 循环获取游标数据,进行累加
set result = CONCAT(result, flag1 ,','); END LOOP;
-- 关闭游标
close cur1;
-- 返回参数
set result = CONCAT(result,'<span style="color:Red">'); RETURN result;
内容扩展:
mysql游标使用的整个过程为:
1.创建游标
DECLARE calc_bonus CURSOR FOR SELECT id, salary, commission FROM employees;
2.打开游标
OPEN calc_bonus;
3.使用游标
FETCH calc_bonus INTO re_id, re_salary, re_comm;
4.关闭游标
CLOSE calc_bonus;