删除程序(如果存在于 mysql 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46856261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:50:14  来源:igfitidea点击:

Drop procedure if exists in mysql

mysqldatabasestored-procedures

提问by Naphstor

Hi i am trying to create a mysql script that I can run whenever I need to update my database. The script creates a table and then executes some stored procedures.

嗨,我正在尝试创建一个可以在需要更新数据库时运行的 mysql 脚本。该脚本创建一个表,然后执行一些存储过程。

DELIMITER $$

CREATE TABLE IF NOT EXISTS tbl_name (
    col1 bigint(20) NOT NULL AUTO_INCREMENT,
    col2  varchar(255) NOT NULL,
    col3 varchar(64) NOT NULL,
    col4 datetime DEFAULT NULL,
    PRIMARY KEY (`col1 `),
    UNIQUE KEY col2  (`col2`)
) ENGINE=InnoDB AUTO_INCREMENT=572 DEFAULT CHARSET=utf8$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
VARCHAR(255))
BEGIN
    DECLARE var1 VARCHAR(64);
    DECLARE expirationDate DATETIME;

    SET var1 = 12345;
    SET expirationDate = DATE_ADD(NOW(), INTERVAL 30 SECOND);

    REPLACE INTO tbl_name (col2, col3, col4) VALUES (someval, var1, expirationDate);
END$$

DELIMITER ;

When I ran the script first time, it created the table and executed the stored procedure in MySQL Workbench. When I ran the same thing second time, I got the error 1304 procedure already exists.

当我第一次运行脚本时,它创建了表并在 MySQL Workbench 中执行了存储过程。当我第二次运行同样的事情时,我得到错误 1304 程序已经存在。

I looked online hereabout dropping the procedure and then create again. But when I entered the below command before creating the procedure, i got an error on CREATEcommand with code 1064.

我在网上查看了有关删除程序然后再次创建的信息。但是,当我在创建过程之前输入以下命令时,我收到CREATE代码为 1064 的命令错误。

DROP PROCEDURE IF EXISTS myproc;
CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
    VARCHAR(255))
.
.
.

I am very new to mysql and not sure how to execute the procedure if it already exists.

我对 mysql 很陌生,如果它已经存在,我不确定如何执行该过程。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Bill Karwin

Since you changed to DELIMITER $$you need to use that delimiter at the end of each statement until you change it back.

由于您更改为 ,因此DELIMITER $$您需要在每个语句的末尾使用该分隔符,直到您将其更改回来。

DROP PROCEDURE and CREATE PROCEDURE are separate statements, and each requires its own statement delimiter.

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

DROP PROCEDURE IF EXISTS myproc $$

Note the delimiter at the end of the line above.

请注意上面一行末尾的分隔符。

CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN username 
    VARCHAR(255))
.
.
.
END $$

And another delimiter at the end of the whole CREATE PROCEDURE statement.

以及整个 CREATE PROCEDURE 语句末尾的另一个分隔符。