mysql存储过程中如何使用游标的实例

雪幕拉开,飘然而至分外美妙,初起便显现出意境;雪落十分,然间有种喜悦在心间荡漾,纷飞飘扬尽享每一刻青春的激情;飞舞的雪花,感受芬芳的韵味,迷人心脾围绕在身旁;耳畔,时常想有沙沙的声音那样的动听,百听不厌,着迷在这一刻升起动人心怀的旋律。


DELIMITER $$

DROP PROCEDURE IF EXISTS getUserInfo $$

CREATE PROCEDURE getUserInfo(in date_day datetime)
--
-- 实例
-- MYSQL存储过程名为:getUserInfo
-- 参数为:date_day日期格式:2008-03-08
--
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 ;

本文mysql存储过程中如何使用游标的实例到此结束。只有品味了痛苦,才能珍视曾经忽略的快乐小编再次感谢大家对我们的支持!

标签: 过程中
  • 开发手册
  • 常用软件