Error near 'DELIMITER $$' - mysql

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:

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

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

Related

MariaDB Create Procedure Error 1064 Syntax

I am getting a syntax error (1064) at line 10 ''. The line 10 is the line with the FROM.
I tried without delimiter. I tried adding ";" after the "stg_mytable". I tried with just "SELECT 1 as cname" in the stored procedure body. I tried in the UI of HeidiSQL instead of commands.
Here's the code:
DELIMITER $$
CREATE PROCEDURE MySP()
BEGIN
INSERT INTO dbo_mytable
(ColumnStr, OtherColumn)
SELECT ColumnStr, OtherColumn
FROM stg_mytable
END $$
DELIMITER ;
Apparently, HeidiSQL and DBeaver have difficulties with delimiters. I have successfully created my stored procedure in MySQL Workbench.

How do I set sql delimiter through R code?

I need to create a trigger in sql server via R code for which I need to set my sql delimiter to //.
I tried doing the following:
dbExecute(con, "delimiter //")
dbExecute(con, "delimiter //\n")
dbExecute(con, "delimiter //\t")
I also tried the above scenarios with other DBI functions like
dbGetQuery and dbSendQuery
but I am getting the following error.
could not run statement: 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
It turns out that in order to execute an sql trigger through R using the DBI package, one does not need to set and unset the delimiter. We can directly execute the trigger command.
This is unlike what needs to be done while setting a triggers through SQL command line where, since the trigger syntax itself includes a semicolon ;, in order to avoid conflict with the default SQL delimiter which is also ; we temporarily set the delimiter to a lesser used special character such as // with a command such as
delimiter //
and then revert back to the default delimiter with
delimiter ;
which need not be done when trigger is executed through DBI package of R.

MySQL stored proc not getting created

So I have this stored proc that will not get created when I run the file.
DELIMITER //
DROP PROCEDURE IF EXISTS msd.test_proc//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM
msd.zipcode_lookup;
END//
DELIMITER ;
When I run this I get an error code 1064 at line 1 when I execute in RazorSQL. Here is the complete error message:
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 '//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM ' at line 1
Error Code:1064
I've tried other variations and still get errors. I am sure this is something basic I am missing. I appreciate any help.
Thanks.
As stated on the RazorSQL website:
The DELIMITER statement is not part of the MySQL language. It is a command supported by certain MySQL tools. This command tells those MySQL programs to scan for a certain character that indicates the end of a query or statement.
RazorSQL does not support using the DELIMITER command. The SQL statement delimiter value used by RazorSQL can be changed using the preferences window. The default values is the semi-colon.

how to solve syntax error in procedure in mysql?

i am executing a procedure in mysql, procedure is-
delimiter $$
create procedure getFacility (in id int, out MyFacility VARCHAR(200))
begin
select Facility into MyFacility
from facilities
where FacilityID = id ;
end $$
delimiter ;
and it is giving error below-
Error Code : 1064
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 'end $$
delimiter' at line 1
(0 ms taken)
how to solve this error?
Thanks,
Yugal
I think the error is because of the space between END and $$.
Try END$$ and it should compile properly.
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
i had the same issue... my solution was to just remove the delimiter operator call on line one, and remove the delimiter after 'END'... it seems JDBC just doesn't like that operator.
it would also be really nice if MYSQL gave some more precision on syntax errors.

create procedure fails?

when trying to create a simple procedure in mysql 5.1.47-community it fails everytime i've tried everything!
even simple things like this!
DELIMITER //
CREATE PROCEDURE two ()
begin
SELECT 1+1;
end;
//
The error is
ERROR: Error 1064: 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 'mydb' at line 1
The error message you've given doesn't correspond to the code you've pasted. You're referring to "mydb" somewhere in the SQL you're running yet it's not anywhere in the code you've put in the question.
The code you've given should work fine as I see no syntax errors, you may just need to give it a database to work on ("test" in my case here, perhaps it should be "mydb" for you?).
DELIMITER //
CREATE PROCEDURE test.two ()
begin
SELECT 1+1;
end;
//
DELIMITER ;
CALL test.two;
However, I suspect the error you're getting is become of a line in your SQL that you're not showing us.
EDIT
It could perhaps be the delimiter command. You're changing the delimiter to // rather than the default ;. So perhaps you've run that command (and changed the delimiter for your session to //), and then tried to run USE mydb; where the ; is no longer recognised as a valid delimiter for your session, and that could be giving you the error. Try putting delimiter ; before your use line and see if that helps (and then use it again after you've defined your stored procedure so you can call it). This is just a theory though, as I'm not sure of the intricacies of the delimiter command.
Remove the final delimiter "end" instead "end;"
I had the same problem using heidisql as the fronted to enter the SQL. My first attempt was:
CREATE PROCEDURE Add_Two (IN someNumber int, OUT result INT)
BEGIN
SELECT someNumber +2 INTO result;
END
and this resulted in SQL ERROR (1064) because i was not aware that when using a client program a delimiter is needed to define the stored procedures.
After changing the above to this:
DELIMITER //
CREATE PROCEDURE Add_Two(IN someNumber int, OUT result INT)
BEGIN
SELECT someNumber +2 INTO result;
END
//
It worked out.
Example to call it
SET #someNumber :=8;
CALL Add_Two(#someNumber, #result);
SELECT #result;