Skip to content

Instantly share code, notes, and snippets.

@juanonsoftware
Last active April 12, 2018 10:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanonsoftware/6a632beeb10460e6f86707aa86a0da88 to your computer and use it in GitHub Desktop.
Save juanonsoftware/6a632beeb10460e6f86707aa86a0da88 to your computer and use it in GitHub Desktop.
To get all jobs that are running on an instance
SELECT
ja.job_id,
j.name AS job_name,
ja.start_execution_date,
ISNULL(last_executed_step_id,0)+1 AS current_executed_step_id,
Js.step_name
FROM msdb.dbo.sysjobactivity ja
LEFT JOIN msdb.dbo.sysjobhistory jh
ON ja.job_history_id = jh.instance_id
JOIN msdb.dbo.sysjobs j
ON ja.job_id = j.job_id
JOIN msdb.dbo.sysjobsteps js
ON ja.job_id = js.job_id
AND ISNULL(ja.last_executed_step_id,0)+1 = js.step_id
WHERE ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)
AND start_execution_date is not null
AND stop_execution_date is null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment