MYSQL存储过程带游标

这篇博客介绍了如何使用SQL创建一个存储过程updateYearAndMonth,该过程通过游标遍历output_incomes表,查找并更新那些year_and_month字段为空的income_id,确保日期格式为'年/月'。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值