Sql-server – Need help trying to run a batch file from a SQL Agent job

command linesql server

I am trying to run a batch file from a SQL Agent job. This batch file is a connection command and script file that will run. I can run the the actions straight from Command Prompt, but I need it to be fired from a SQL Agent job for the automation of it.

Every time I try to fire it off I get these errors:

"The system cannot find the file specified"

When I set the command up, I am using the GUI in the Jobs area and using the 'Open' button and then navigating to the proper file. I have also tried changing the Services from the NT user to a Local Admin user, but still had the same end result.

Best Answer

The 'Open' button is for the convenience of importing a large script into the command window, which is not helpful with this type of job step as I explain below. If you want it to run a batch file, you simply enter the path and filename, such as:

D:\scripts\DoSomething.bat

or

D:\executables\DoSomething.exe

And be sure to specify the type of command as "Operating System (CmdExec)"

Whatever account the step is set to run as will need permission to execute the batch file or process as well as access to any resources that are used in the script or process.

Also, a quirk of this type of job step is that it will only run one command. So if you put the following in the command input box:

rem This script does something
D:\scripts\DoSomething.bat

It will run the first command and then stop, so the batch file will not be run. So that is why the 'Open' button is not really helpful for CmdExec job steps.