When I make a stored procedure with SQLYog, it gives me this template:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `apachelogs`.`test`()
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
END$$
DELIMITER ;
I wonder why it changes delimiter before creating stored procedure. And why I can still use ;
delimiter inside of create
statement(I guess it may because of scope).
That changes the statement delimiter to $$
to keep MySQL from trying to interpret any semicolons inside the stored procedure as a delimiter. Without the DELIMITER $$
, you wouldn't be able to define a stored procedure that contained ;
to delimit statements inside it. You can think of it as a way to globally escape the semicolon for the duration of the procedure's definition.
From the fine manual:
To redefine the mysql delimiter, use the
delimiter
command. The following example shows how to do this for thedorepeat()
procedure just shown. The delimiter is changed to//
to enable the entire definition to be passed to the server as a single statement, and then restored to;
before invoking the procedure. This enables the;
delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
I don't know if my explanation is any clearer than the manual's but maybe one of them (or both combined) will result in some understanding.