Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all stored procedures in MySQL or using temporary stored procedures

Tags:

Is there a statement that can drop all stored procedures in MySQL? Alternatively (if the first one is not possible), is there such thing as temporary stored procedures in MySQL? Something similar to temporary tables?

like image 909
Kenston Choi Avatar asked Jun 12 '10 07:06

Kenston Choi


People also ask

How do I drop all functions in mysql?

To drop a function, you must have the DELETE privilege for the mysql database. This is because DROP FUNCTION removes the row from the mysql. func system table that records the function's name, type and shared library name. For dropping a stored function, see DROP FUNCTION.

How can I drop stored procedure?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure to remove, and then click Delete. To view objects that depend on the procedure, click Show Dependencies.


2 Answers

I would have thought that this would do it, but I'm open to corrections:

(EDITED to incorporate a good point provided in the comments)

delete from mysql.proc WHERE db LIKE <yourDbName>;

(As pointed out by Balmipour in the comments below, it's a good idea to specify the database.)

I think it's valid to want to drop all procedures in a given database, otherwise in a long development cycle there's a danger of obsolete procedures and functions accumulating and muddying everything up.

like image 79
user1070300 Avatar answered Sep 18 '22 15:09

user1070300


Since DROP PROCEDURE and DROP FUNCTION does not allow sub selects, I thought it might be possible to perform the operation through another stored procedure, but alas, MySQL does not allow stored procedures to drop other stored procedures.

I tried to trick MySQL to to this anyway by creating prepared statements and thus separating the drop call somewhat from the stored procedure, but I've had no luck.

So therefore my only contribution is this select statement which creates a list of the statements needed to drop all stored procedures and functions.

SELECT
    CONCAT('DROP ',ROUTINE_TYPE,' `',ROUTINE_SCHEMA,'`.`',ROUTINE_NAME,'`;') as stmt
FROM information_schema.ROUTINES;
like image 37
Ivar Bonsaksen Avatar answered Sep 16 '22 15:09

Ivar Bonsaksen