MySQL 如果MySQL中不存在,如何说创建过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9948713/
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 12:47:04  来源:igfitidea点击:

how to say create procedure if not exist in MySQL

mysqlstored-proceduresexists

提问by Frank

I am trying to write a script to create a procedure in MySQL database, but I want to check if it exsit first.

我正在尝试编写一个脚本来在 MySQL 数据库中创建一个过程,但我想先检查它是否存在。

I know how to do it for a table, but when I use the same syntax for stored procedure, it doesn't compile.

我知道如何为表执行此操作,但是当我对存储过程使用相同的语法时,它无法编译。

Does anybody know? Thanks

有人知道吗?谢谢

回答by Cory Klein

Just drop the procedure if it does exist and then re-add it:

如果它确实存在,只需删除该过程,然后重新添加它:

DROP PROCEDURE IF EXISTS my_procedure;
CREATE PROCEDURE my_procedure()

回答by Ben English

SELECT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name');

So you could do:

所以你可以这样做:

IF NOT EXISTS(SELECT 1 FROM mysql.proc p WHERE db = 'db_name' AND name = 'stored_proc_name') THEN
....
END IF;

回答by robertvoliva

It doesn't exist. We had to write a stored procedure that mimics the same functionality. Basically we create stored procedures by calling a stored procedure that does the "if exists" check.

它不存在。我们必须编写一个模拟相同功能的存储过程。基本上,我们通过调用执行“如果存在”检查的存储过程来创建存储过程。