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

先上代码


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声明完所有 变量 ,包括游标等 之后再执行,
否则该存储过程将无法运行
正确方法

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值