首页 文章

为什么“丢弃过程如果存在konami”不能在mysql中工作?

提问于
浏览 1068
0

我写了一个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();

但是当我将命令“drop procedure if exists 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 workbench 6.3中运行第二个脚本时 .

It report error : Error Code: 1304. PROCEDURE konami already exists

所以我的问题是当我将命令“drop procedure if exists konami”移动到程序konami()的前面时,为什么这个语句“如果存在konami的话程序”不工作?

1 回答

  • 1

    由于您更改为DELIMITER //您需要在每个语句的末尾使用该分隔符,直到您将其更改为止 .

    DROP PROCEDURE和CREATE PROCEDURE是单独的语句,每个语句都需要自己的语句分隔符 .

    DROP PROCEDURE IF EXISTS konami //
    

相关问题