how to solve syntax error in procedure in mysql? - 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.

Related

SQL 1064 in Procedures

Hey I'm leraning right now for my study Exams.
I have a Problem with Procedures and Functions.
CREATE PROCEDURE testProc()
BEGIN
DECLARE testNR INT;
END
This is my testCode but every Procedure and every Function gives me this Failure:
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 '' at line 3
SQLState: 42000
ErrorCode: 1064
Error occurred in:
CREATE PROCEDURE testProc()
BEGIN
DECLARE testNR INT
Is there something that I do wrong or do I have Problems with my IDE SQuirrel?
You need to change delimiters while creating the procedure; otherwise ; is seen as the end of the CREATE PROCEDURE statement.
As explained in the docs, you need to
use[] the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. See Section 23.1, “Defining Stored Programs”.
It doesn’t have to be //; another common choice is $$.
In your example, this might look like
DELIMITER $$
CREATE PROCEDURE testProc()
BEGIN
DECLARE testNR INT;
END
$$
DELIMITER ;

simple mysql trigger, cannot find the syntax error. appreciate for it

I am using phpmyadmin and got a syntax error from my trigger part. I followed examples online but didn't see anything wrong. Does anyone see the syntax error? Thank you in advance.
Here is my code:
CREATE TRIGGER INSERT_T BEFORE INSERT ON dataT
for each row begin
IF(NEW.id IS NOT NULL)
SET NEW.id = 9999;
END IF
end;
mysql reports that:
#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 'SET NEW.id = 9999' at line 4
.
Missing THEN keyword.
MySQL Reference Manual:
IF Syntax https://dev.mysql.com/doc/refman/5.7/en/if.html
And for your next questions. Yes, you need a semicolon following the END IF
And you need to specify a delimiter other than the default semicolon. e.g.
DELIMITER $$
CREATE TRIGGER ...
END$$
DELIMITER ;

Syntax error in mysql new version

I have a problem with this, and I keep getting this error
MySQL said: Documentation
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 '' at line 5.
Do you have any ideas for why I keep getting this error?
DROP PROCEDURE IF EXISTS `prn_insert`;
CREATE PROCEDURE `prn_insert`(id int, name text, description text)
BEGIN
insert into test
select id,name,description;
END
The semicolon is ending the CREATE PROCEDURE statement. To get the entire statement, use a delimiter other than a semicolon. We frequently use $$ (two dollar signs) as a delimiter, but you can use any character sequence that doesn't appear within the statement(s) you want to execute.
For example:
DELIMITER $$
DROP PROCEDURE myproc $$
CREATE PROCEDURE myproc(arg INT)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = 1;
END$$
DELIMITER ;
Once the new delimiter is set, it stays in effect until it's changed to something else. So. we usually want to set it back to semicolon immediately after the `CREATE PROCEDURE' statement.

Error near 'DELIMITER $$'

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

mysql changing delimiter

I'm trying to add this function using php myadmin, first off I get on error line 5, which is apparently because you need to change the delimiter from ; to something else so i tried this
DELIMITER |
CREATE FUNCTION LEVENSHTEIN (s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS INT
DETERMINISTIC
BEGIN
etc....
END
DELIMITER ;
But I get this 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 'delimiter |
Please help !?
solved it by installing new version of phpmyadmin, which has a delimiter box!