View stored procedure/function definition in MySQL

前端 未结 8 665
鱼传尺愫
鱼传尺愫 2020-12-23 02:14

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 SH

相关标签:
8条回答
  • 2020-12-23 02:35

    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' ;
    
    0 讨论(0)
  • 2020-12-23 02:38
    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.

    0 讨论(0)
  • 2020-12-23 02:40

    You can use this:

    SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
    WHERE ROUTINE_SCHEMA = 'yourdb' AND ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = "procedurename";
    
    0 讨论(0)
  • 2020-12-23 02:41
    SHOW CREATE PROCEDURE proc_name;
    

    returns the definition of proc_name

    0 讨论(0)
  • 2020-12-23 02:47

    Perfect, try it:

    SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
        WHERE ROUTINE_SCHEMA = 'yourdb' AND ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = "procedurename";
    
    0 讨论(0)
  • 2020-12-23 02:50

    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... ;)

    0 讨论(0)
提交回复
热议问题