范例:
BEGIN
-- 定义参数
declare carId int(10);
declare v_count int;
declare done int default 1;
-- 一个游标(cursor)可以被看作指向结果集(a set of rows)中一行的指针(pointer)
declare logisticCarInfoCursor cursor for select id from logistic_car_info;
select count(1) into v_count from logistic_car_info;
-- 开始提交
start transaction;
-- 打开结果集
open logisticCarInfoCursor;
-- 循环结果中的数量
while done < v_count +1 do
-- 游标是好几个值,并且可以有N条记录,fetch游标into到变量里
fetch logisticCarInfoCursor into carId;
-- 最终将物流信息表中的数据更新为已支付的状态
update logistic_car_execute_report lcer ,trading_record tr
set tr.pay_state=1, account_state=1 where lcer.car_id=carId and tr.trad_code=lcer.batch_id and lcer.pay_flag='1' and state='4' and tr.pay_state=0;
--执行一次数据加一
set done= done +1;
--结束循环
end while;
--关闭游标
close logisticCarInfoCursor;、
--提交
COMMIT;
END