Error near 'DELIMITER $$'

邮差的信 提交于 2019-12-03 17:22:46

DELIMITER is actually a MySQL command line setting, not SQL: http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html. That means that you can't set the delimiter in this way.

On top of that, it wouldn't help if you could as ActiveRecord::Base.connection.execute only allows you to execute one statement at a time out of the box (see http://www.seanr.ca/tech/?p=75).

quetzaluz

The top answer is correct (Rails cannot execute DELIMITER because it is a MYSQL command), but @ishandutta2007's follow up question was not answered, so I'll answer that here.

DELIMITER is often used to wrap mysql function and procedure bodies; to achieve this in rails simply wrap the procedure body in it's own execute statement.

So for instance code that might read like:

execute <<-SQL
  DROP FUNCTION IF EXISTS MyFunc;
  DELIMITER $$
  CREATE FUNCTION My Func
    . . .
  $$
  DELIMITER ;
SQL

Would instead become the following, with the multiple execute calls acting as the 'scoping' intended by redefining the delimiter:

execute 'DROP FUNCTION IF EXISTS MyFunc'
execute <<-SQL
  CREATE FUNCTION My Func
    . . .
SQL
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!