MariaDB
 sql >> Database >  >> RDS >> MariaDB

Come elencare tutte le stored procedure in MariaDB

In MariaDB, possiamo usare SHOW PROCEDURE STATUS comando per restituire un elenco di stored procedure.

Possiamo anche interrogare information_schema.routines tabella per fare la stessa cosa.

Il SHOW PROCEDURE STATUS Comando

Il modo più semplice per elencare tutte le stored procedure è utilizzare SHOW PROCEDURE STATUS comando.

Basta eseguire quanto segue per elencare tutte le stored procedure:

SHOW PROCEDURE STATUS;

La sintassi è questa:

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

Quindi puoi usare un LIKE clausola o WHERE clausola per restringere i risultati.

Esempio:

SHOW PROCEDURE STATUS LIKE 'film%';

Risultato:

+--------+-------------------+-----------+------------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| 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 |
+--------+-------------------+-----------+------------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+

Il information_schema.routines Tabella

Un altro modo per ottenere un elenco di stored procedure è eseguire una query su information_schema.routines tabella.

Esempio:

SELECT 
    routine_schema as "Database",
    routine_name
FROM 
    information_schema.routines
WHERE 
    routine_type = 'PROCEDURE'
ORDER BY 
    routine_schema ASC, 
    routine_name ASC;

Risultato:

+----------+--------------------+
| Database | routine_name       |
+----------+--------------------+
| mysql    | AddGeometryColumn  |
| mysql    | DropGeometryColumn |
| pethouse | spGetAllPets       |
| pethouse | spGetPetById       |
| sakila   | film_in_stock      |
| sakila   | film_not_in_stock  |
| sakila   | rewards_report     |
+----------+--------------------+

Questa tabella memorizza anche informazioni sulle funzioni memorizzate. Nell'esempio sopra, li ho esclusi usando un WHERE clausola per restituire solo stored procedure (ovvero oggetti con un routine_type di PROCEDURE ).

Per includere le funzioni memorizzate possiamo rimuovere il WHERE clausola:

SELECT 
    routine_schema as "Database",
    routine_name,
    routine_type
FROM 
    information_schema.routines
ORDER BY 
    routine_schema ASC, 
    routine_name ASC;

Risultato:

+----------+----------------------------+--------------+
| 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    |
+----------+----------------------------+--------------+

In questo caso ho anche aggiunto il routine_type colonna in modo da poter distinguere tra le procedure e le funzioni.

Possiamo anche escludere alcuni database dal risultato se vogliamo:

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;

Risultato:

+----------+----------------------------+--------------+
| 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    |
+----------+----------------------------+--------------+

Oppure potremmo restringerlo a un determinato database:

SELECT 
    routine_schema as "Database",
    routine_name,
    routine_type
FROM 
    information_schema.routines
WHERE 
    routine_schema = 'pethouse'
ORDER BY 
    routine_name ASC;

Risultato:

+----------+--------------+--------------+
| Database | routine_name | routine_type |
+----------+--------------+--------------+
| pethouse | spGetAllPets | PROCEDURE    |
| pethouse | spGetPetById | PROCEDURE    |
+----------+--------------+--------------+