Search code examples
mysqlsqlstored-proceduresphpmyadminsql-scripts

SQL script create stored procedure - importing error


My SQL script (sproc.sql):

use timetable_db;

DROP PROCEDURE IF EXISTS GetEmptyClassrooms;

delimeter |
CREATE PROCEDURE GetEmptyClassrooms()
 BEGIN
   SELECT room_number,seats from classrooms WHERE room_number not in (SELECT room_number FROM timetable);
 END |

When I try to import it to MySQL via the phpmyadmin GUI I got the errors:

Unexpected beginning of command. (near "delimiter" at position 0)

Sorry, the default language is not English in my MySQL. I tried to translate as precisely as I can.

I've read several posts here in S.overflow about similar errors but I couldn't solve it.

There are solutions to solve this, but not much help because in the end I want to use only the "my db name . sql" script including all the create tables, insert rows...etc and of course the create procedures, everything in one, simply importable file.

I tried:

  • remove the delimiter statements, leaving the '|' or '//' or '$$' there
  • remove delimiter and delimiter character using ';' instead of '|' or '//' or '$$'
  • removing delimiters and ;
  • removing only ;
  • removing tabs -> using only spaces
  • trying the only the "SELECT ..." statement in mysql, it worked so I don't think it would be the problem.

I'm really a beginner in SQL but I want to improve myself.

Thanks for the help :)


Solution

  • I think there is error in syntax which you are using. Please try below way.

       DROP procedure IF EXISTS `GetEmptyClassrooms`;
            DELIMITER $$
            USE `test`$$
            CREATE PROCEDURE `GetEmptyClassrooms` ()
            BEGIN
             SELECT room_number,seats from classrooms
     WHERE room_number not in (SELECT room_number FROM timetable);
            END
            $$
            DELIMITER ;
    

    where test is the database name where you want to create the stored procedure