復制代碼 代碼如下:
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 ;