web-dev-qa-db-fra.com

Afficher le code de procédure dans MySQL

J'ai essayé de montrer le code SQL d'une procédure stockée appelée add_pixel:

mysql> SHOW PROCEDURE CODE add_pixel;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Je me suis assuré que la procédure existe:

mysql> CALL add_pixel();
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE pixels.add_pixel; expected 3, got 0

Et SHOW CREATE PROCEDURE travaux:

mysql> SHOW CREATE PROCEDURE add_pixel;
+-----------+----------+------------------+----------------------+----------------------+--------------------+
| Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation |
+-----------+----------+------------------+----------------------+----------------------+--------------------+
| add_pixel |          | NULL             | utf8                 | utf8_general_ci      | utf8_unicode_ci    |
+-----------+----------+------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

et que ma version MySQL prend en charge la procédure de spectacle ( Afficher le code de procédure a été ajoutée dans MySQL 5.0.17 ).

Comment puis-je voir le code SQL source de la procédure?

6
Adam Matan

ÉDITER

Incroyablement assez, INFORMATION_SCHEMA.ROUTINES n'a pas de colonne param-list.

Vous pouvez utiliser mysql.proc. Par exemple, en regardant une procédure de mine:

SELECT * FROM mysql.proc WHERE name='run' \G
*************************** 1. row ***************************
                  db: common_schema
                name: run
                type: PROCEDURE
       specific_name: run
            language: SQL
     sql_data_access: MODIFIES_SQL_DATA
    is_deterministic: YES
       security_type: INVOKER
          param_list:
  in query_script text

             returns:
                body: main_body: begin
  if (LEFT(query_script, 1) in ('/', '\\')) and (LEFT(query_script, 2) != '/*') then
    begin
          declare query_script_file_name text;

      set query_script_file_name := query_script;
      set query_script := LOAD_FILE(query_script_file_name);
      if query_script is null then
        call throw(CONCAT('Cannot load script file: ', query_script_file_name));
      end if;
    end;
  end if;
  call _interpret(query_script, TRUE);
end
             definer: root@localhost
             created: 2012-08-17 17:24:46
            modified: 2012-08-17 17:24:46
            sql_mode:
             comment: Run given QueryScript text
character_set_client: utf8
collation_connection: utf8_general_ci
        db_collation: latin1_swedish_ci
           body_utf8: main_body: begin
  if (LEFT(query_script, 1) in ('/', '\')) and (LEFT(query_script, 2) != '/*') then
    begin
          declare query_script_file_name text;

      set query_script_file_name := query_script;
      set query_script := LOAD_FILE(query_script_file_name);
      if query_script is null then
        call throw(CONCAT('Cannot load script file: ', query_script_file_name));
      end if;
    end;
  end if;
  call _interpret(query_script, TRUE);
end

Cherchez le param_list colonne (je crois que c'était votre principale préoccupation). Cherchez également db, name, type.

==================

Alternativement, peut utiliser INFORMATION_SCHEMA.ROUTINES. Par exemple, en regardant une procédure de mine:

SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME='run' \G
*************************** 1. row ***************************
       SPECIFIC_NAME: run
     ROUTINE_CATALOG: NULL
      ROUTINE_SCHEMA: common_schema
        ROUTINE_NAME: run
        ROUTINE_TYPE: PROCEDURE
      DTD_IDENTIFIER: NULL
        ROUTINE_BODY: SQL
  ROUTINE_DEFINITION: main_body: begin
  if (LEFT(query_script, 1) in ('/', '\')) then
    begin
          declare query_script_file_name text;

      set query_script_file_name := query_script;
      set query_script := LOAD_FILE(query_script_file_name);
      if query_script is null then
        call throw(CONCAT('Cannot load script file: ', query_script_file_name));
      end if;
    end;
  end if;
  call _interpret(query_script, TRUE);
end
       EXTERNAL_NAME: NULL
   EXTERNAL_LANGUAGE: NULL
     PARAMETER_STYLE: SQL
    IS_DETERMINISTIC: YES
     SQL_DATA_ACCESS: MODIFIES SQL DATA
            SQL_PATH: NULL
       SECURITY_TYPE: INVOKER
             CREATED: 2012-08-17 17:24:46
        LAST_ALTERED: 2012-08-17 17:24:46
            SQL_MODE: 
     ROUTINE_COMMENT: Run given QueryScript text
             DEFINER: root@localhost
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
  DATABASE_COLLATION: latin1_swedish_ci

En particulier, regardez les colonnes ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_DEFINITION. Ceux-ci fournissent la plupart des données intéressantes.

11
Shlomi Noach