Nedenfor er to muligheder for at returnere en komplet liste over lagrede procedurer i MySQL.
SHOW PROCEDURE STATUS
Kommando
Den hurtigste måde at få vist 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
eller WHERE
klausul for at indsnævre resultaterne.
Eksempel:
SHOW PROCEDURE STATUS LIKE 'albums%';
Resultat:
+-------+------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+ | Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation | +-------+------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+ | Music | spAlbumsByGenre | PROCEDURE | [email protected] | 2021-11-13 13:09:26 | 2021-11-13 13:09:26 | DEFINER | | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | | Music | spAlbumsFromArtistId | PROCEDURE | [email protected] | 2021-11-13 13:03:25 | 2021-11-13 13:03:25 | DEFINER | | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | | Music | spAlbumsFromArtistName | PROCEDURE | [email protected] | 2021-11-13 13:03:48 | 2021-11-13 13:03:48 | DEFINER | | utf8mb4 | utf8mb4_0900_ai_ci | utf8mb4_0900_ai_ci | +-------+------------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
information_schema.routines
Tabel
En anden måde at få en liste over lagrede procedurer i MySQL 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;
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;
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:
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;
Her er, hvad dette returnerer i mit testmiljø:
+----------+------------------------+--------------+ | Database | ROUTINE_NAME | ROUTINE_TYPE | +----------+------------------------+--------------+ | Music | spAlbumsByGenre | PROCEDURE | | Music | spAlbumsFromArtistId | PROCEDURE | | Music | spAlbumsFromArtistName | PROCEDURE | | Music | spArtistFromId | PROCEDURE | | PetHotel | spGetAllPets | PROCEDURE | | PetHotel | spGetPetById | PROCEDURE | +----------+------------------------+--------------+
Vi kan også indsnævre det til en bestemt database:
SELECT
routine_schema as "Database",
routine_name,
routine_type
FROM
information_schema.routines
WHERE
routine_schema = 'music'
ORDER BY
routine_name ASC;
Resultat:
+----------+------------------------+--------------+ | Database | ROUTINE_NAME | ROUTINE_TYPE | +----------+------------------------+--------------+ | Music | spAlbumsByGenre | PROCEDURE | | Music | spAlbumsFromArtistId | PROCEDURE | | Music | spAlbumsFromArtistName | PROCEDURE | | Music | spArtistFromId | PROCEDURE | +----------+------------------------+--------------+