Problem:
Every database person might have come across the situation of maintenance tasks such as backing up of databases, re-indexing tables and other such tasks. We often schedule jobs for such tasks, so that they execute as per the set schedule. But there is sometimes the need for these tasks to be executed “On Demand”. This tip shows various ways of executing such tasks on demand by any user regardless of whether the person is technical or not.
Solution
Let’s say I have a job called “BACKUPTEST” which backups the test databases. I want to be able to execute the job “On Demand”, so whenever anyone needs to do the backup this can be done. In this article I will show you how you can execute such Jobs easily through various ways.
In this tip we will look at these four methods:
SQL Server Management Studio
T-SQL commands
DMO (Distributed Management Objects)
OSQL
Also, this tip assumes that the jobs have already been setup.
The first way that most people are probably aware of is to use SQL Server Management Studio.
SQL Server Agent is the job scheduling tool for SQL Server.
To execute a job on demand using the GUI, open the SQL Server Agent tree, expand Jobs, select the job you want to run, right click on that job and click ‘Start Job’ and the job will execute.
The second way is through a T-SQL statement using the 'sp_start_job' stored procedure which instructs SQL Server Agent to execute a job immediately. It is a stored procedure in the 'msdb' database.
The syntax for the sp_start_job stored procedure is:
[@job_name] | [@job_id ] Is the name of the job to start. Either job_id or job_name must be specified, but both cannot be specified. job_name is sysname, with a default of NULL.
[@error_flag =] error_flag Reserved.
[@server_name =] 'server_name' Is the target server on which to start the job. server_name is nvarchar(30), with a default of NULL. server_name must be one of the target servers to which the job is currently targeted.
[@step_name =] 'step_name' Is the name of the step at which to begin execution of the job. Applies only to local jobs. step_name is sysname, with a default of NULL
[@output_flag =] output_flag Reserved.
When a job run it will have one of two return codes:
0 (success)
1 (failure)
To run the job ‘BACKUPTEST’ it can be executed by a single T-SQL statement: such as:
Another way of executing the job is through a VBS script using Distributed Management Objects (DMO).
Here is the basic script syntax.
On Error Goto 0: Main()
Sub Main()
Set objSQL = CreateObject("SQLDMO.SQLServer")
' Leave as trusted connection
objSQL.LoginSecure = True
' Change to match the name of your SQL server
objSQL.Connect "Enter Server Name Here"
Set objJob = objSQL.JobServer
For each job in objJob.Jobs
if instr(1,job.Name,"Enter Job Name Here") > 0 then
msgbox job.Name
job.Start
msgbox "Job Started"
end if
Next
End Sub
Here is sample executing the "BACKUPTEST" job on server "SQLTEST1". This uses NT authentication to run this script.
On Error Goto 0: Main()
Sub Main()
Set objSQL = CreateObject("SQLDMO.SQLServer")
' Leave as trusted connection
objSQL.LoginSecure = True
' Change to match the name of your SQL server
objSQL.Connect "SQLTEST1"
Set objJob = objSQL.JobServer
For each job in objJob.Jobs
if instr(1,job.Name,"BACKUPTEST") > 0 then
msgbox job.Name
job.Start
msgbox "Job Started"
end if
Next
End Sub
This code would then be saved in a file and named something like "RunJob.vbs". You can then double click on the file to execute it or run the code from a command line.