您的位置:首页 > 数据库 > MySQL

mysql存储过程的坑(关于declare与具体实现顺序)

2017-09-29 13:24 288 查看
先上代码

BEGIN

DECLARE hotelId BIGINT DEFAULT 0;
DECLARE done int DEFAULT FALSE;
DECLARE tbtypeId BIGINT DEFAULT 0;

SELECT id into tbtypeId from dbbanquet.tbtype where fname = '早餐';
/* 声明游标 */
declare myCursor cursor for SELECT hotel_id from kzsteb.hotel where hotel_id not in (SELECT distinct fhid from dbbanquet.tbtype);
/* 当游标到达尾部时,mysql自动设置done=1 */
declare continue handler for not found set done=true;open myCursor;

read_loop:LOOPfetch myCursor into hotelId;
if done = true then
leave read_loop;
end if;
insert into dbbanquet.tbbanquet(fname,funit,funitprice,ftype,findex,fmanagement,fhid,fcu,fcid,fctime,fmu,fmid,fmtime)
VALUES('包价早餐','*',0,tbtypeId,1,0,hotelId,'',1,NOW(),'',0,NOW());
end loop;
close myCursor;
END


错误原因:

存储过程的所有关于数据库增删改查的操作必须在所有 declarre声明完所有 变量 ,包括游标等 之后再执行,

否则该存储过程将无法运行

正确方法将SELECT id into tbtypeId from dbbanquet.tbtype where fname = '早餐';

该句放到 declare continue handler for not found set done=atrue;之后








                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: