There is often a need for end-users to run SQL Jobs on an ad-hoc basis. This creates a security issue because we don't want to install SQL Server Management Studio on the end-user workstation and we don't want to give out Remote Desktop access to the SQL Server or grant additional security within SQL.
Here is a quick process that solves these issues and allows an end-user to run a SQL Server Agent Job from a Windows BAT file on their workstation:
On the User’s machine:
ECHO Executing job
ECHO.
CD C:\SQLBATCH
sqlcmd -S "[SQL SERVER NAME]" -E -Q"exec msdb.dbo.sp_start_job '[SQL JOB NAME]'"
ECHO Job execution completed
pause
CLS
EXIT
NOTE: the BAT file will return a successful message indicating the job was started. It doesn't tell you if the job had errors. An administrator on SQL still needs to monitor the jobs for errors.
Create a folder named SQLBATCH on the user’s machine. Then create a file named SQLJOBSTART.bat
Enter the following, replacing names where necessary. Then save the file :
ECHO Executing job
ECHO.
sqlcmd -S "VM-SQL1" -Q "exec msdb.dbo.sp_start_job 'Sage 500 ERP sage500_app QtyOn'"
ECHO Job execution completed
pause
CLS
EXIT
The above shows an example of a batch file that will execute the Sage 500 QtyOn job on machine named VM-SQL1. For details about the switches used by the sqlcmd, type sqlcmd /? from a command window. The results from the execution run appear below.
NOTE: the BAT file will return a successful message indicating the job was started. It doesn't tell you if the job had errors. An administrator on SQL still needs to monitor the jobs for errors.