Процедуры Mysql

Материал из rrv-wiki
Перейти к навигации Перейти к поиску

Когда пишешь процедуру, например:

CREATE PROCEDURE nowPlayUpdateGorvolna(IN songTitle VARCHAR(255))
SQL SECURITY INVOKER
BEGIN
 SET @now = now();
 INSERT INTO `now_playing_gorvolna` (`title`, `count`, `updated`) VALUES (songTitle, 1, @now)
 ON DUPLICATE KEY UPDATE
   `updated` = @now,
   `count` = `count` + 1;
END

получаем ошибки:

mysql> CREATE PROCEDURE nowPlayUpdateGorvolna(IN songTitle VARCHAR(255))
   -> SQL SECURITY INVOKER
   -> BEGIN
   ->   SET @now = now();
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 4
mysql>   INSERT INTO `now_playing_gorvolna` (`title`, `count`, `updated`) VALUES (songTitle, 1, @now)
   ->   ON DUPLICATE KEY UPDATE
   ->     `updated` = @now,
   ->     `count` = `count` + 1;
ERROR 1146 (42S02): Table 'uniton_new.now_playing_gorvolna' doesn't exist

Используем замену перевода строки delimiter:

mysql> delimiter |
mysql> CREATE PROCEDURE nowPlayUpdateGorvolna(IN songTitle VARCHAR(255))
   -> SQL SECURITY INVOKER
   -> BEGIN
   ->   SET @now = now();
   ->   INSERT INTO `now_playing_gorvolna` (`title`, `count`, `updated`) VALUES (songTitle, 1, @now)
   ->   ON DUPLICATE KEY UPDATE
   ->     `updated` = @now,
   ->     `count` = `count` + 1;
   -> END|
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;

и все получается.