user470760
user470760

Reputation:

Executing an SQL file from BAT script

I have created a batch script which is supposed to execute the SQL file in the same directory and then pause, where I connect to MYSQL and issue the commands inside of the SQL file. For some reason when I directly paste these lines into a command window, it works fine. When I have it setup as follows, I receive an Access Denied error. Can anyone please point out what I am doing wrong?

BAT File:

cmd < script.sql
pause

Script.SQL file

@echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname
SELECT `SERVER_ID`
FROM tc_services
WHERE  `GAME_ID` LIKE  '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
exit

Upvotes: 2

Views: 5365

Answers (2)

RolandoMySQLDBA
RolandoMySQLDBA

Reputation: 44343

You have the files organized incorrectly. Please redo the file as follows:

BAT File:

@echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname < script.sql
pause

script.sql file

SELECT `SERVER_ID`
FROM tc_services
WHERE  `GAME_ID` LIKE  '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';

Give it a Try !!!

Upvotes: 1

Garry Brantley
Garry Brantley

Reputation: 1

You did not say what operating system you are using. It looks like you are trying to write your output file to the "\Program Files (x86)" directory. As a security measure Windows does not allow a user to write to that file area. You will need to go to the security settings of the "D:\Program Files (x86)\TCAdmin Control Panel\ScheduledTasks\GAME\" folder and make sure the user running the bat/script file has write access to that folder.

Upvotes: 0

Related Questions