View stored procedure/function definition in MySQL

MysqlStored ProceduresStored Functions

Mysql Problem Overview


What is the MySQL command to view the definition of a stored procedure or function, similar to sp_helptext in Microsoft SQL Server?

I know that SHOW PROCEDURE STATUS will display the list of the procedures available. I need to see a single procedure's definition.

Mysql Solutions


Solution 1 - Mysql

SHOW CREATE PROCEDURE <name>

Returns the text of a previously defined stored procedure that was created using the CREATE PROCEDURE statement. Swap PROCEDURE for FUNCTION for a stored function.

Solution 2 - Mysql

You can use this:

SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA = 'yourdb' AND ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = "procedurename";

Solution 3 - Mysql

SHOW CREATE PROCEDURE proc_name;

returns the definition of proc_name

Solution 4 - Mysql

If you want to know the list of procedures you can run the following command -

show procedure status;

It will give you the list of procedures and their definers Then you can run the show create procedure <procedurename>;

Solution 5 - Mysql

You can use table proc in database mysql:

mysql> SELECT body FROM mysql.proc
WHERE db = 'yourdb' AND name = 'procedurename' ;

Note that you must have a grant for select to mysql.proc:

mysql> GRANT SELECT ON mysql.proc TO 'youruser'@'yourhost' IDENTIFIED BY 'yourpass' ;

Solution 6 - Mysql

something like:

DELIMITER //

CREATE PROCEDURE alluser()
BEGIN
   SELECT *
   FROM users;
END //

DELIMITER ;

than:

SHOW CREATE PROCEDURE alluser

gives result:

'alluser', 'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER', 'CREATE DEFINER=`root`@`localhost` PROCEDURE `alluser`()
BEGIN
   SELECT *
   FROM users;
END'

Solution 7 - Mysql

An alternative quick and hacky solution if you want to get an overview of all the produres there are, or run into the issue of only getting the procedure header shown by SHOW CREATE PROCEDURE:

mysqldump --user=<user> -p --no-data --routines <database>

It will export the table descriptions as well, but no data. Works well for sniffing around unknown or forgotten schemas... ;)

Solution 8 - Mysql

Perfect, try it:

SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_SCHEMA = 'yourdb' AND ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = "procedurename";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrinivas M.V.View Question on Stackoverflow
Solution 1 - MysqlaffteeView Answer on Stackoverflow
Solution 2 - MysqlHaim EvgiView Answer on Stackoverflow
Solution 3 - MysqlvalliView Answer on Stackoverflow
Solution 4 - MysqlSibashish PujariView Answer on Stackoverflow
Solution 5 - MysqlOmidreza BagheriView Answer on Stackoverflow
Solution 6 - MysqlMichelView Answer on Stackoverflow
Solution 7 - MysqlISparkesView Answer on Stackoverflow
Solution 8 - MysqlSumitView Answer on Stackoverflow