执行如下存储过程:
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1 BEGIN DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT '' ; DECLARE i INT DEFAULT 0; WHILE i < n DO SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1)); SET i = i +1; END WHILE; RETURN return_str; END;
出现错误:原因是没有修改定界符:
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
默认情况下,delimiter ";" 用于向mysql提交查询语句。在执行存储过程</a>中每个语句的结尾都有个";",这个时候每逢";"就像mysql提交的话,当然会出问题。于是在写存储过程代码之前,先修改mysql的存储定界符delimiter,等存储过程执行之后,在修改回";"
比如修改为"//";
代码示例:delimiter //
执行存储过程:
delimiter // CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1 BEGIN DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT '' ; DECLARE i INT DEFAULT 0; WHILE i < n DO SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1)); SET i = i +1; END WHILE; RETURN return_str; END;//
执行完记着修改delimiter
delimiter ;
新的错误出现了
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
解决办法:
创建子程序(存储过程、函数、触发器)时,声明为DETERMINISTIC或NO SQL与READS SQL DATA中的一个,
就是在begin关键在之前声明,我这里声明为DETERMINISTIC
完整代码:
delimiter // CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1 DETERMINISTIC BEGIN DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT '' ; DECLARE i INT DEFAULT 0; WHILE i < n DO SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1)); SET i = i +1; END WHILE; RETURN return_str; END;//
在执行一次发现完美执行
转载请注明:谷谷点程序 » mysql存储过程:delimiter定界符导致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