create procedure updateYearAndMonth()
BEGIN
-- 定义参数
declare id int;
declare yearMonth varchar(10);
declare done INT DEFAULT FALSE;
-- 定义游标
declare cur_income cursor for
select income_id,date_format(posting_date,'%Y/%m') from output_income where
year_and_month is null;
-- 将结束标志绑定到游标
declare continue handler for not found set done = TRUE;
open cur_income;
-- 循环游标
read_loop:LOOP
fetch cur_income into id,yearMonth;
IF done THEN LEAVE read_loop;
END IF;
update output_income set year_and_month=yearMonth where income_id=id;
END LOOP;
close cur_income;
end
-- 调用
call updateYearAndMonth()