Detecting The State of a SQL Server Agent Job | Database Journal

Detecting The State of a SQL Server Agent Job

Written By
Gregory Larsen
Gregory Larsen
Mar 31, 2005
4 minute read

How do you determine if a
SQL Server Agent Job is running? Is there a way to automate the process of
determining if a SQL Server Agent job is running? What automated approach is
there to determine what step is being processed for a given SQL Server Agent
job? Is there an easy way to determine all of the SQL Server Agent jobs that
are currently running? This article will show you a couple of different methods
to determine the state of an individual job, or all SQL Server Agent jobs.

Why Would you want to do this?

So why would someone what
this information? Maybe you just want to know if any SQL Server Agent jobs
are running, before you shutdown SQL Server. Or, possibly you want to start a
particular SQL Server Agent job, but want to make sure that the jobs is not
already running, prior to submitting the job again. Possible you want to know
what step is currently executing for a particular running SQL Server Agent job.
Maybe you have an automated process that needs to be able to detect if a job is
running or not, so it can perform some action.

Using Enterprise Manager

I am sure most are familiar
with using Enterprise Manager to review the current status of all SQL Server
agent jobs. For this reason, I am not going to spend much time discussing
this option. To use Enterprise Manager, you just need to expand the server
folder, and then expand the “Management” folder, followed by expanding the “SQL
Server Agent” folder and finally expanding the “Jobs” folder. After doing this
you can use the “Status” column to determine whether a job is running, and what
step is currently executing.

Using sp_help_job

The stored procedure (SP) “sp_help_job”
is a SP that can be used to return SQL Server Agent job information. This SP
comes with SQL Server and is stored in the “msdb” database. This SP returns a
different record set depending on what parameters are passed. I am not going
to touch on all of the different ways to call this SP, so if you are interested
in the complete functionality of this SP, I would suggest you review the
information about this SP in the “SQL Server Books Online” documentation. What
I am going to touch on is how to use the sp_help_job to return
information about the status of a single SQL Agent job, or all SQL Agent jobs
on an instance of SQL Server.

Advertisement

To return the SQL Server
Agent job information for all jobs on a server you can use the following
command:

exec msdb.dbo.sp_help_job

This command returns a record
set that contains one row for each SQL Server Agent job. The table
below explains what each column represents in the returned record
set:

Column nameData typeDescription
job_iduniqueidentifierUnique ID of the job.
originating_servernvarchar(30)Name of the server from
which the job came.
NamesysnameName of the job.
EnabledtinyintIndicates whether the job is
enabled to be executed.
Descriptionnvarchar(512)Description for the job.
start_step_idIntID of the step in the job
where execution should begin.
CategorysysnameJob category.
OwnersysnameJob owner.
notify_level_eventlogIntBitmask indicating under
what circumstances a notification event should be logged to the Microsoft
Windows NT. application log. Can be one of these values:

0 = Never
1 = When a job succeeds
2 = When the job fails
3 = Whenever the job completes (regardless of the job outcome)

notify_level_emailIntBitmask indicating under
what circumstances a notification e-mail should be sent when a job completes.
Possible values are the same as for notify_level_eventlog.
notify_level_netsendIntBitmask indicating under
what circumstances a network message should be sent when a job completes.
Possible values are the same as for notify_level_eventlog.
notify_level_pageIntBitmask indicating under
what circumstances a page should be sent when a job completes. Possible
values are the same as for notify_level_eventlog.
notify_email_operatorsysnameE-mail name of the
operator to notify.
notify_netsend_operatorsysnameName of the computer or
user used when sending network messages.
notify_page_operatorsysnameName of the computer or
user used when sending a page.
delete_levelIntBitmask indicating under
what circumstances the job should be deleted when a job completes. Possible
values are the same as for notify_level_eventlog.
date_createddatetimeDate the job was created.
date_modifieddatetimeDate the job was last
modified.
version_numberIntVersion of the job
(automatically updated each time the job is modified).
last_run_dateIntDate the job last started
execution.
last_run_timeIntTime the job last started
execution.
last_run_outcomeIntOutcome of the job the
last time it ran:

0 = Failed
1 = Succeeded
3 = Canceled
5 = Unknown

next_run_dateIntDate the job is scheduled
to run next.
next_run_timeIntTime the job is scheduled
to run next.
next_run_schedule_idIntIdentification number of
the next run schedule.
current_execution_statusIntCurrent execution status.
current_execution_stepSysnameCurrent execution step in
the job.
current_retry_attemptIntIf the job is running and
the step has been retried, this is the current retry attempt.
has_stepIntNumber of job steps the
job has.
has_scheduleIntNumber of job schedules
the job has.
has_targetIntNumber of target servers
the job has.
TypeInt1 = Local job.
2 = Multiserver job.
0 = Job has no target servers.

The table above is from the
“SQL Server Books Online” documentation. Note that there is a column returned named
“current_execution_status” which identifies the current status of each job. Below
is a table from the “SQL Server Books Online” documentation that describes the
meaning of each status:

ValueDescription
1Executing.
2Waiting for thread.
3Between retries.
4Idle.
5Suspended.
7Performing completion
actions.
Gregory Larsen

Gregory A. Larsen is a DBA at Washington State Department of Health (DOH). Greg is responsible for maintaining SQL Server and other database management software. Greg works with customers and developers to design and implement database changes, and solve database/application related problems. Greg builds homegrown solutions to simplify and streamline common database management tasks, such as capacity management.

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.