问题
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