MySQL delimiter syntax error - mysql

I'm trying to change the MySQL command delimiter so I can create a procedure with multiple commands in it. However, the delimiter command does not seem to be recognised on MySQL 5.1.47. I tested it on MySQL 5.0.91, and it did work there.
DELIMITER //;
DELIMITER ;//
I'm trying to run this from phpmyadmin, in both situations. Using 5.0.91 instead isn't an option because I need to use events (CREATE EVENT).
Error message:
#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 //' at line 1
Is there a reason it's not working, or is there an alternative to accomplish the same thing (creating a procedure with multiple queries)?

DELIMITER is not a MySQL command. It's a command that your MySQL client needs to support. I was running PHPMyAdmin 2.8.2.4, which didn't support it. When I upgraded to the newest version, which is currently 3.4.9, it worked just fine. Your MySQL version has nothing to do with DELIMITER and whether it's supported or not.

You don't need to delimit the DELIMIT statements
DELIMITER //
procedure here etc
DELIMITER ;
Exactly as per "Defining Stored Programs" in the MySQL docs.
And if you can control versions, the latest is 5.5.20. Why not use that?
Edit:
The error message indicates an error in the previous statement... if this can't be seem force it thus
; /* <- force previous statement termination */ DELIMITER //
procedure here etc
DELIMITER ;

Related

MySQL Workbench - Stored Procedure - Error - No reference in manual

I am using MySQL workbench 8.0.18 on a mac.
I am trying to run the following stored procedure using $$ as my delimiter. It is not running and giving me the error
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 '' at line 3
And here is the code with the delimiter:
delimiter $$
create procedure HelloWorld()
begin
select 'Hello World!';
end $$
I have gone through the documentation for mysql workbench but could not find any relevant answer pertaining to it.
Thank you for your help in advance.
You have to run this in a NORMAL query tab when you want to use DELIMITER:
If you want to add a procdure in new_procedure Routine
Leave out the DELIMITER completely,
When you save the procedure Workbench will add the DELIMITER abd DEFINER automatically

White space changes the delimiter when running MySQL script from the shell/command prompt

I use MySQL innodb version 5.7.10 'MySQL Community Server (GPL)' on a Windows machine. The following script runs fine if I run it from MySQL Workbench:
DROP PROCEDURE IF EXISTS procedure1;
DELIMITER //
CREATE PROCEDURE procedure1(IN pageSize BIGINT)
BEGIN
SELECT * FROM table1 LIMIT pageSize;
END //
DELIMITER ; -- note that there is an extra tab char here
DROP PROCEDURE IF EXISTS procedure2;
DELIMITER //
CREATE PROCEDURE procedure2()
BEGIN
SELECT * FROM table2;
END //
DELIMITER ;
But if copy the script into schema.sql and run it from Windows command prompt:
mysql> c:\release\ver1\schema.sql
I get the following 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 //
This error is because of the tab char, if I remove the tab, it will run fine.
I have also tried running it from Linux shell (MyQSL innodb version 5.7.25) and I get the same error:
mysql> source /home/ubuntu/release/ver1/schema.sql
Note: I have tried replacing the tab char (\t) with a couple of white space characters, and the white spaces are fine... it's only the tab char which changes the delimiter.
I am using MySQL Workbench for the Dev Environment, but for Test and Prod Environments, I am just using MySQL from Linux Shell... this error has caught me so many times, because the scripts which have passed the Dev Environment, fails in Test and every time I have to remember to go back and remove the tabs from the script.
Is there anyway to fix this issue or configure MySQL to ignore tab char?
There are programs like GrepWin that can easily replace characters in files. However, if you want something automated, I would recommend you setup a webhook and have a command to replace all tabs like below execute every time your schema is pushed.
sed -i.bak $'s/\t*//g' schema.sql

mysql using tee in stored procedure

I'm new to MySQL (or SQL in general)
I'm trying to get MySQL to write the timestamp into a file with stored procedure using TEE command (I don't think I can use "select into outfile" because I don't want to delete the file, I want to add a line to it...):
mysql> DELIMITER $$
mysql> CREATE PROCEDURE test_to_file()
-> begin
-> TEE /home/ubuntu/test.txt;
-> SELECT NOW();
-> end $$
However, I get an error:
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 '/home/ubuntu/test.txt;
SELECT NOW();
end' at line 3
Thanks for the help
Disclaimer: I am not certain that whatever you are trying to do is necessarily a good idea... but there is a simple solution for this.
Create a table using the CSV Storage Engine.
You can then append to the file by simply inserting into this table.
Using tee is a client-side feature. It doesn't write to the server at all, unless you happen to be running the mysql client on the server machine... in which case, it's only writing to the server machine by coincidence.

creating stored procedure in ubuntu terminal

I am trying to create stored procedures in mysql. I am doing it on terminal (ubunutu).
But when I am adding ; before end it executes all lines and throw an error .
I am trying to write
create procedure test()
-> begin
-> select * from salary;
I am unable to add end in last and it executeds and got this error
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 3.
Is this a right way to write stored procedure in terminal ?
Can I write stored procedure on separate file?
Then how can I execute that stored procedure written in file ?
fortunately i found a way to write a stored procedures on terminal
http://alextsilverstein.com/programming-and-development/mysql/mysql-the-reason-for-using-the-delimiter-statement-in-mysql-routines-stored-procedures-functions-triggers/

mysql: DELIMITER syntax error at line 1

I try to add this function using the following SQL in phpmyadmin/MySQL
DROP FUNCTION IF EXISTS `__myv`;
DELIMITER ;;
CREATE FUNCTION `__myv`(`a` int, `b` int) RETURNS bigint(20)
BEGIN
return FLOOR(a / b);
END;;
DELIMITER ;
but I get this error:
Error
SQL query:
DELIMITER;
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 'DELIMITER' at line 1
How to fix this error??
DELIMITER is a Mysql console command, You can't use it in phpmyadmin.
To set the delimiter in phpmyadmin, see this other answer
Even if DELIMITER is a console command, phpMyAdmin's import module has accepted it since many years. When opening a database and clicking on SQL, a query entered there is passed to the import module, so it should work (unless you have a very old phpMyAdmin version).