Mysql – Stored Procedure imported from file, but tables have no data

insertmysql-5.7stored-procedures

Here is the Stored Procedure (import from file) that I'm having trouble:

DELIMITER //
CREATE PROCEDURE `books_database`()
BEGIN
  DECLARE n INT DEFAULT 0;
  WHILE n < 5 DO
    INSERT INTO books.science (Title,Author) VALUES ('Data Structures Using C','Aaron Tenenbaum');
    INSERT INTO books.science (Title,Author) VALUES ('C Programming Language','Brian W. Kernighan, Dennis Ritchie');
    INSERT INTO books.science (Title,Author) VALUES ('My Inventions','Nikolas Tesla');
    SET n = n + 1;
  END WHILE;
END //

When I import the SP file, it finishes without error. When I check database/table, there is no data inserted from the SP.

What is lacking or wrong with this Stored Procedure?

Best Answer

The fact of importing or creating a Stored Procedure, does not include its execution.

After creating the SP (via MySQL Shell or via bootstrap file) you must call it, in order to process the routines which are stored in it.

Stored Procedure

DELIMITER //
CREATE PROCEDURE `books_database`()
BEGIN
   SELECT...
   INSERT...
END //

Call your Stored Procedure...

CALL books_database();



Extra Comment: Creation of Stored Procedures via MySQL Shell

  • Before creating your SP, first, select the database (use your_database) where this SP will be used, or else, as you try to create the SP, the MySQL Shell will return: ERROR 1046 (3D000): No database selected.
  • Don't forget to rollback to the default delimiter (DELIMITER ;) in order to perform the normal commands on MySQL Shell, after creating your SP.