Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 920
Hooman Bahreini Avatar asked Nov 18 '19 00:11

Hooman Bahreini


People also ask

What does MySQL prompt indicates?

When you issue a query, mysql sends it to the server for execution and displays the results, then prints another mysql> prompt to indicate that it is ready for another query. mysql displays query output in tabular form (rows and columns).

What flag do you use when specifying the host on the MySQL command?

For example, -h localhost or --host=localhost indicate the MySQL server host to a client program. The option value tells the program the name of the host where the MySQL server is running. The first command instructs mysql to use a password value of test , but specifies no default database.

Is MySQL Shell and MySQL command line client same?

The MySQL Shell is a big advancement over the old command line client. First, it has three dialects — SQL, Python, and JavaScript. If you have libraries or code in either Python or JavaScript to use on your data then you can use them.


1 Answers

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
like image 103
Chibueze Opata Avatar answered Sep 26 '22 02:09

Chibueze Opata