how to say create procedure if not exist in MySQL

半腔热情 提交于 2019-12-18 18:40:02

问题


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

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


回答1:


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

DROP PROCEDURE IF EXISTS my_procedure;
CREATE PROCEDURE my_procedure()



回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/9948713/how-to-say-create-procedure-if-not-exist-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!