在 MariaDB 中列出所有存储过程

本文讨论在 MariaDB 中列出所有存储过程的两种方法。

发布于

在 MariaDB 中,我们可以使用两种方法/命令获取所有的存储过程:

  1. 使用 SHOW PROCEDURE STATUS 命令获取存储过程列表。
  2. information_schema.routines 表中查询存储过程列表。

SHOW PROCEDURE STATUS 用法

只需运行以下 SHOW PROCEDURE STATUS 命令即可列出所有存储过程:

SHOW PROCEDURE STATUS;

如果您需要过滤其中的部分数据,请使用 LIKE 子句或者 WHERE 子句,如下:

SHOW PROCEDURE STATUS
    [LIKE 'pattern' | WHERE expr]

示例

要查看 Sakila 示例数据库中的所有存储过程,请使用以下带有 WHERE 子句的语句:

SHOW PROCEDURE STATUS WHERE db = 'sakila';

输出:

+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+--------------------------------------------------+----------------------+----------------------+--------------------+
| Db     | Name              | Type      | Definer        | Modified            | Created             | Security_type | Comment                                          | character_set_client | collation_connection | Database Collation |
+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+--------------------------------------------------+----------------------+----------------------+--------------------+
| sakila | film_in_stock     | PROCEDURE | root@localhost | 2022-12-12 15:07:32 | 2022-12-12 15:07:32 | DEFINER       |                                                  | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
| sakila | film_not_in_stock | PROCEDURE | root@localhost | 2022-12-12 15:07:32 | 2022-12-12 15:07:32 | DEFINER       |                                                  | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
| sakila | rewards_report    | PROCEDURE | root@localhost | 2022-12-12 15:07:32 | 2022-12-12 15:07:32 | DEFINER       | Provides a customizable report on best customers | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+--------------------------------------------------+----------------------+----------------------+--------------------+
3 rows in set (0.014 sec)

要查看名称以 film 开头的所有存储过程,请使用以下带有 LIKE 子句的语句:

SHOW PROCEDURE STATUS LIKE 'film%';

输出:

+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db     | Name              | Type      | Definer        | Modified            | Created             | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| sakila | film_in_stock     | PROCEDURE | root@localhost | 2022-12-12 15:07:32 | 2022-12-12 15:07:32 | DEFINER       |         | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
| sakila | film_not_in_stock | PROCEDURE | root@localhost | 2022-12-12 15:07:32 | 2022-12-12 15:07:32 | DEFINER       |         | utf8mb4              | utf8mb4_general_ci   | utf8mb4_general_ci |
+--------+-------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
2 rows in set (0.015 sec)

使用 information_schema.routines

您还可以从 information_schema.routines 表中获取存储过程列表。 information_schema.routines 是一个系统表,其中存储了数据库中存储过程和函数的信息。

您需要使用 routine_type 指定要查询的类型:

  • PROCEDURE: 存储过程
  • FUNCTION: 函数

比如,要查询 Sakila 示例数据库中的所有存储过程,请使用以下语句:

SELECT
  routine_schema,
  routine_name
FROM
  information_schema.routines
WHERE routine_schema = 'sakila'
  AND routine_type = 'PROCEDURE';

输出:

+----------------+-------------------+
| routine_schema | routine_name      |
+----------------+-------------------+
| sakila         | film_in_stock     |
| sakila         | film_not_in_stock |
| sakila         | rewards_report    |
+----------------+-------------------+
3 rows in set (0.006 sec)

结论

本文讨论了在 MariaDB 中列出所有存储过程的两种方法。