createtable gifts ( id int unsigned auto_increment, tid int unsigned notnulldefault'0', price decimal(8,2) notnulldefault'0.0', name varchar(32) notnulldefault "" comment '名称',
drop procedure if exists sync_price; delimiter $$ create procedure sync_price() begin declare giftId int; declare tplId int; declare tplPrice decimal(8,2); declare isDone int default 0; declare giftCursor cursor for select id, tid from gifts; -- 声明游标 declare continue handler for not found set isDone = 1; -- 捕获 not found,设置isDone=1 表示循环完毕. open giftCursor; read_loop: loop -- 开始循环 fetch giftCursor into giftId, tplId; if isDone then leave read_loop; end if; select price into tplPrice from template where id = tplId; select giftId, tplId, tplPrice; -- just for test update gifts set price = tplPrice where id = giftId; end loop; close giftCursor; end; $$ delimiter ;