7

when I change Delimeter from mysql console or MySQL Workbench I do not get any error, but when I embed the same code in ruby on rails I get error

mysql> DELIMITER $$
mysql>

gives no error.

but

ActiveRecord::Base.connection.execute(%Q{
    DELIMITER $$
})

gives:

ActiveRecord::StatementInvalid: Mysql2::Error: 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 'DELIMITER $$' at line 1: 
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129

2 Answers2

5

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
Community
  • 1
  • 1
quetzaluz
  • 1,071
  • 12
  • 17
3

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).

Jon Cairns
  • 11,783
  • 4
  • 39
  • 66
  • 4
    then how do I execute the following from rails?[CODE] DELIMITER $$ CREATE PROCEDURE XYZ(IN t INT) BEGIN BLA BLA ... END$$ DELIMITER ;[/CODE] – ishandutta2007 Mar 27 '13 at 11:12