Tuesday, November 2, 2010

Execute SQL file using a batch file

My task was to run a batch file for executing a SQLfile (CreateTables.sql) containing queries to create database, some tables and their fields.


I’ve installed MYSQL Server in my PC.


Initially check whether MYSQL environment variables have been set. If not, set the path using,
Right click My Computer --> Properties --> Advanced --> Environment Variables --> System Variables --> Click on the PATH variable --> Click on Edit --> At the end of Variable Value, add the following
C:\Program Files\MySQL\MySQL Server 5.0\bin;
(The bin path of your MYSQL Server)



Initially create a batch file using the following steps.


Step 1: Open a notepad window.
Step 2: Type the following commands.


@echo off
echo please wait........


mysql --user=root --password=admin<CreateTables.sql



mysql --user=YourMYSQLUsername --password= YourMYSQLPassword <Your SQL filename
(In case your SQL file and batch files are in different folder, you have to specify the full path of SQL file.
Eg:- mysql --user=YourMYSQLUsername --password= YourMYSQLPassword <D:\Folder1\Your SQL filename )



Step 3: Save the filename as “filename.bat” (Include the filename within double quotes)
Step 4: Double click the batch file.



While executing the batch file, the following output will be displayed. 








Note: If you are using Windows XP, make sure that the batch file is not in your desktop.

No comments:

Post a Comment

Thanks for visiting my Blog....Ur comments n suggestions are most valuable for me...Thanks