因为mysql存储过程导致sql文件执行出错

今天在公司做项目,使用 sqlyog 导入sql脚本文件时报错,打开sqlyog.err.txt后,发现报错如下:

Error occured at:2019-05-05 10:49:35
Line no.:185
Error Code: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3Query:
CREATE PROCEDURE `usercenter`.`setNubeProc`()
BEGIN
DECLARE	v_nubeNumber varchar(20)

于是使用notepad++打开sql脚本文件,根据提示,出错的位置在第185行,而且是语法错误,截取报错部分如下:

DROP PROCEDURE IF EXISTS `usercenter`.`setNubeProc`;
CREATE PROCEDURE `usercenter`.`setNubeProc`()
BEGIN
DECLARE	v_nubeNumber varchar(20);
DECLARE	v_uid varchar(64)  default '';
declare count int default 0;
DECLARE userinfo_cur CURSOR  for  select uid from usercenter.user_info where nube_number is  null and mobile is not null  and account_status = '0';
DECLARE	nube_cur CURSOR  for  select nubeNumber from nubeservice.nube_number where nubeNumber like  '9%' and status = '1' LIMIT 1;
declare continue handler for not found set v_uid = null; 
open userinfo_cur;
FETCH userinfo_cur INTO v_uid;
WHILE (v_uid is not null ) DO
	set count := count + 1;
	open nube_cur;
	FETCH nube_cur INTO v_nubeNumber;
	CLOSE nube_cur;
	update usercenter.user_info set nube_number = v_nubeNumber where uid = v_uid;
	update nubeservice.nube_number set status = '2' where nubeNumber = v_nubeNumber;
	FETCH userinfo_cur INTO v_uid;
end while;
CLOSE userinfo_cur;
select count;
END;

因为是语法错误,而且又是在创建存储过程中报的错,可以定位为存储过程语法错误。

解决办法为声明 delimiter $$,以"$$"取代以";"作为语句结束的标志,注意这是临时修改,使用完毕后要改回来。

修改后的脚本文件如下:

DROP PROCEDURE IF EXISTS `usercenter`.`setNubeProc`;
delimiter $$
CREATE PROCEDURE `usercenter`.`setNubeProc`()
BEGIN
DECLARE	v_nubeNumber varchar(20);
DECLARE	v_uid varchar(64)  default '';
declare count int default 0;
DECLARE userinfo_cur CURSOR  for  select uid from usercenter.user_info where nube_number is  null and mobile is not null  and account_status = '0';
DECLARE	nube_cur CURSOR  for  select nubeNumber from nubeservice.nube_number where nubeNumber like  '9%' and status = '1' LIMIT 1;
declare continue handler for not found set v_uid = null; 
open userinfo_cur;
FETCH userinfo_cur INTO v_uid;
WHILE (v_uid is not null ) DO
	set count := count + 1;
	open nube_cur;
	FETCH nube_cur INTO v_nubeNumber;
	CLOSE nube_cur;
	update usercenter.user_info set nube_number = v_nubeNumber where uid = v_uid;
	update nubeservice.nube_number set status = '2' where nubeNumber = v_nubeNumber;
	FETCH userinfo_cur INTO v_uid;
end while;
CLOSE userinfo_cur;
select count;
END$$

delimiter ;

重新执行后没有报错,至此问题解决。 

以上就是本人因为mysql存储过程导致sql脚本执行出错的解决办法。

你可能感兴趣的:(sql脚本执行出错,mysql存储过程,sqlyog,mysql)