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

Sådan listes alle lagrede procedurer i MariaDB

I MariaDB kan vi bruge SHOW PROCEDURE STATUS kommando for at returnere en liste over lagrede procedurer.

Vi kan også forespørge på information_schema.routines bordet for at gøre det samme.

SHOW PROCEDURE STATUS Kommando

Den nemmeste måde at liste alle lagrede procedurer på er at bruge SHOW PROCEDURE STATUS kommando.

Kør blot følgende for at få en liste over alle lagrede procedurer:

SHOW PROCEDURE STATUS;

Syntaksen ser sådan ud:

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

Så du kan bruge en LIKE klausul eller WHERE klausul for at indsnævre resultaterne.

Eksempel:

SHOW PROCEDURE STATUS LIKE 'film%';

Resultat:

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

information_schema.routines Tabel

En anden måde at få en liste over lagrede procedurer på er at forespørge information_schema.routines tabel.

Eksempel:

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

Resultat:

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

Denne tabel gemmer også oplysninger om lagrede funktioner. I ovenstående eksempel ekskluderede jeg dem ved at bruge en WHERE klausul for kun at returnere lagrede procedurer (dvs. objekter med en routine_type af PROCEDURE ).

For at inkludere lagrede funktioner kan vi fjerne WHERE klausul:

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

Resultat:

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

I dette tilfælde tilføjede jeg også routine_type kolonne, så vi kan skelne mellem procedurerne og funktionerne.

Vi kan også udelukke visse databaser fra resultatet, hvis vi ønsker:

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;

Resultat:

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

Eller vi kunne indsnævre det til en given database:

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

Resultat:

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

  1. SQL-forespørgsel returnerer data fra flere tabeller

  2. SQL server ignorer store og små bogstaver i et where-udtryk

  3. Sådan konfigureres databasemail i SQL Server

  4. hvis min tabel har 4 kolonner, og jeg vil hente den 3. kolonne, hvad gør jeg.