为什么“删除程序如果存在konami”在mysql中不起作用?

时间:2018-12-10 09:28:06

标签: mysql

我写了一个mysql脚本,它工作正常:

use cloud_fight;

drop procedure if exists `konami`;

drop function if exists `rand_string`;

delimiter //  
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255)
BEGIN
     DECLARE chars_str varchar(255) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789rew';
     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()*64 ),1));
         SET i = i + 1;
     END while;
     RETURN return_str;
 END //

delimiter $$
create procedure konami() 
begin
declare q int default 1;
while q<=2 DO
set @N=rand_string(64);
insert into fight_user (user_guid , first_name , last_name , user_name , nick_name , gender , city , province , country , head_img_url , created_at , last_updated) values (@N, '' , '' , '' , rand_string(5) , '2' , 'bj' , 'bj' , 'cn' , '' , now() , now());
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'WECHAT_UNIONID' , now() , now() , 1 from fight_user where user_guid = @N;
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'MP_OPENID' , now() , now() , 1 from fight_user where user_guid = @N;
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'PA_OPENID' , now() , now() , 1 from fight_user where user_guid = @N;
set q=q+1;
end while;
end $$
call konami();

但是当我将命令“删除过程如果存在konami”移到过程konami()的前面时,如下所示:

drop function if exists `rand_string`;
delimiter //  
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255)
BEGIN
     DECLARE chars_str varchar(255) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789rew';
     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()*64 ),1));
         SET i = i + 1;
     END while;
     RETURN return_str;
 END //

drop procedure if exists `konami`;
delimiter $$
create procedure konami() 
begin
declare q int default 1;
while q<=2 DO
set @N=rand_string(64);
insert into fight_user (user_guid , first_name , last_name , user_name , nick_name , gender , city , province , country , head_img_url , created_at , last_updated) values (@N, '' , '' , '' , rand_string(5) , '2' , 'bj' , 'bj' , 'cn' , '' , now() , now());
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'WECHAT_UNIONID' , now() , now() , 1 from fight_user where user_guid = @N;
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'MP_OPENID' , now() , now() , 1 from fight_user where user_guid = @N;
insert into fight_user_cake (fight_user_id , t_id , third_party_user_id , type , created_at , last_updated , status) select id , '2' , rand_string(20) , 'PA_OPENID' , now() , now() , 1 from fight_user where user_guid = @N;
set q=q+1;
end while;
end $$
call konami();

当我在mysql工作台6.3中运行第二个脚本时。

它报告错误:错误代码:1304。PROKEDURE konami已存在

所以我的问题是,当我将命令“如果存在konami删除程序”移到程序konami()的前面时,为什么该语句“如果存在konami删除程序”不起作用?

1 个答案:

答案 0 :(得分:1)

由于您已更改为DELIMITER //,因此您需要在每个语句的末尾使用该定界符,直到您将其改回为止。

DROP PROCEDURE和CREATE PROCEDURE是单独的语句,并且每个语句都需要有自己的语句定界符。

DROP PROCEDURE IF EXISTS konami //
相关问题