Page Banner

Batch execute mysql scripts from Windows NT shell

Ever had to execute hundreds of sql scripts for the initial setup of some schema? This nifty one-liner will sort things out for you if you’re working on Windows platform:

FOR %%X IN (*.sql) DO mysql -u DB_USERNAME -pDB_PASS -h localhost SCHEMA_NAME< %%X

What this will do is, it’ll look for all the filenames with an extension of “.sql” in the current directory and log into the mysql server using username “DB_USERNAME” and password “DB_PASS” with the server host located at “localhost”.

Replace the username, password and host fields in the script with the credentials you use to log in to your database server.

Now the important thing to keep in mind is that this script will execute *all* files with and extension “.sql”, so if you want to avoid errors popping up while executing the scripts I’d suggest you to keep your tables details, constraints and triggers in separate folders and execute this batch script separately in each folder and you should be good to go!