0

I'm trying to figure out if a particular SQL Scheduled job is currently running on SQL Server 12.0.5000.0.

I was thinking I could do code similar to the following:

declare @jobid UNIQUEIDENTIFIER

SELECT @jobid = job_id
FROM
msdb.dbo.sysjobs
where name = 'My Job Name'

select * FROM msdb.dbo.sysjobactivity where job_id=@jobid
and start_execution_date is not null and stop_execution_date is null

It does find my running job. Also if I stop the job, I see the stop_execution_date gets populated. However, I have a bunch of other entries in the sysjobactivity that have a populated start_execution_date and a NULL stop_execution_date. Do you know what these could be?

Thanks

Eric
  • 2,861
  • 6
  • 28
  • 59
  • By the way, do you know how to write a joined query? – Charlieface Jul 27 '21 at 18:55
  • You mean like a LEFT OUTER JOIN, that type of thing? I'm still trying to figure out out to figure out which jobs are currently running. That still seems a bit tricky. Looks like it's probably easier to do in C#, calling the sp_help_job – Eric Jul 28 '21 at 10:15
  • I strongly suggest you learn basic joins, it's the first step to knowing anything in SQL. `select * FROM msdb.dbo.sysjobactivity a join msdb.dbo.sysjobs j on where j.job_id = a.jobid where j.name = 'My Job Name' and a.start_execution_date is not null and a.stop_execution_date is null` – Charlieface Jul 28 '21 at 10:23

0 Answers0