En MariaDB, podemos usar SHOW PROCEDURE STATUS
comando para devolver una lista de procedimientos almacenados.
También podemos consultar el information_schema.routines
mesa para hacer lo mismo.
El SHOW PROCEDURE STATUS
Comando
La forma más fácil de enumerar todos los procedimientos almacenados es usar SHOW PROCEDURE STATUS
comando.
Simplemente ejecute lo siguiente para enumerar todos los procedimientos almacenados:
SHOW PROCEDURE STATUS;
La sintaxis es así:
SHOW PROCEDURE STATUS
[LIKE 'pattern' | WHERE expr]
Entonces puedes usar un LIKE
cláusula o WHERE
cláusula para reducir los resultados.
Ejemplo:
SHOW PROCEDURE STATUS LIKE 'film%';
Resultado:
+--------+-------------------+-----------+------------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+ | Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation | +--------+-------------------+-----------+------------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+ | sakila | film_in_stock | PROCEDURE | [email protected] | 2021-11-13 07:26:47 | 2021-11-13 07:26:47 | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8mb4_general_ci | | sakila | film_not_in_stock | PROCEDURE | [email protected] | 2021-11-13 07:26:47 | 2021-11-13 07:26:47 | DEFINER | | utf8mb4 | utf8mb4_general_ci | utf8mb4_general_ci | +--------+-------------------+-----------+------------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
El information_schema.routines
Mesa
Otra forma de obtener una lista de procedimientos almacenados es consultar information_schema.routines
mesa.
Ejemplo:
SELECT
routine_schema as "Database",
routine_name
FROM
information_schema.routines
WHERE
routine_type = 'PROCEDURE'
ORDER BY
routine_schema ASC,
routine_name ASC;
Resultado:
+----------+--------------------+ | Database | routine_name | +----------+--------------------+ | mysql | AddGeometryColumn | | mysql | DropGeometryColumn | | pethouse | spGetAllPets | | pethouse | spGetPetById | | sakila | film_in_stock | | sakila | film_not_in_stock | | sakila | rewards_report | +----------+--------------------+
Esta tabla también almacena información sobre las funciones almacenadas. En el ejemplo anterior, los excluí usando un WHERE
cláusula para devolver solo procedimientos almacenados (es decir, objetos con un routine_type
de PROCEDURE
).
Para incluir funciones almacenadas podemos eliminar WHERE
cláusula:
SELECT
routine_schema as "Database",
routine_name,
routine_type
FROM
information_schema.routines
ORDER BY
routine_schema ASC,
routine_name ASC;
Resultado:
+----------+----------------------------+--------------+ | Database | routine_name | routine_type | +----------+----------------------------+--------------+ | mysql | AddGeometryColumn | PROCEDURE | | mysql | DropGeometryColumn | PROCEDURE | | pethouse | spGetAllPets | PROCEDURE | | pethouse | spGetPetById | PROCEDURE | | sakila | film_in_stock | PROCEDURE | | sakila | film_not_in_stock | PROCEDURE | | sakila | get_customer_balance | FUNCTION | | sakila | inventory_held_by_customer | FUNCTION | | sakila | inventory_in_stock | FUNCTION | | sakila | rewards_report | PROCEDURE | +----------+----------------------------+--------------+
En este caso también agregué el routine_type
columna para que podamos distinguir entre los procedimientos y funciones.
También podemos excluir ciertas bases de datos del resultado si queremos:
SELECT
routine_schema as "Database",
routine_name,
routine_type
FROM
information_schema.routines
WHERE
routine_schema NOT IN ('sys', 'information_schema', 'mysql', 'performance_schema')
ORDER BY
routine_schema ASC,
routine_name ASC;
Resultado:
+----------+----------------------------+--------------+ | Database | routine_name | routine_type | +----------+----------------------------+--------------+ | pethouse | spGetAllPets | PROCEDURE | | pethouse | spGetPetById | PROCEDURE | | sakila | film_in_stock | PROCEDURE | | sakila | film_not_in_stock | PROCEDURE | | sakila | get_customer_balance | FUNCTION | | sakila | inventory_held_by_customer | FUNCTION | | sakila | inventory_in_stock | FUNCTION | | sakila | rewards_report | PROCEDURE | +----------+----------------------------+--------------+
O podríamos reducirlo a una base de datos dada:
SELECT
routine_schema as "Database",
routine_name,
routine_type
FROM
information_schema.routines
WHERE
routine_schema = 'pethouse'
ORDER BY
routine_name ASC;
Resultado:
+----------+--------------+--------------+ | Database | routine_name | routine_type | +----------+--------------+--------------+ | pethouse | spGetAllPets | PROCEDURE | | pethouse | spGetPetById | PROCEDURE | +----------+--------------+--------------+