Scripting in the MySQL Command line environment
01 Jul 2010 Leave a Comment
in Uncategorized Tags: linux, mysql, scripting
In Oracle, I’m used to the ed command to create scripts and the @ operator to run them. You can work in a similar way with MySQL:
To create a script from within the MySQL environment use:
\! vi test.sql
.. there’s a space after the exclamation mark and this assumes that you like the vi editor. Essentially it opens up vi so you can create the test.sql file. If you store all of your scripts in a particular directory (e.g. /home/dave/sql) then it may help if you start mysql in that location.
\! Essentially lets you enter shell commands so if you use HOST from within oracle SQL Plus, you can get the same effect in MySQL by keying:
\! sh
Once you’ve keyed in your script, you can run it using the following syntax:
\. test.sql
..again, there’s a space after the full stop and you need to remember the .sql suffix when running and editing the script.
The script can contain multiple statements, for example the following works fine as the contents of the test.sql file.
use mydb ;select * from cust ;