Checking Job Execution Status with TSQL

  • Hello,

        I am looking for a way to determine (in a stored prcoedure) if a job is running before executing the rest of the stored procedure - if the job is already running, don't start it again...

        The only way I can figure out how to determine this is using sp_help_job. This doesn't lend itself to checking to see if it is executing programatically.

        Is there another method to see if the job is running? I'd love to be able to do something like an if exists statement, looking for a execution status that indicates a job is executing.

     

     

  • sysjobhistory.run_status. A 4 indicates it is running.

  • I already checked sysjobhistory. It only updates that table after the job completes. I even tested it by runing a job and checking the status in sysjobhistory, which didn't change until the job had completed.

    It seems like this should be fairly straightforward, but I can't find a view, table, system variable, etc. to look at to see if a particular job is currently running.

  • Is this what you're looking for ?

    select J.Name as JobName, RP.program_name

     from msdb..sysjobs J with (nolock)

      inner join  master..sysprocesses RP with (nolock)

          on RP.program_name like 'SQLAgent - TSQL JobStep (Job ' + master.dbo.fn_varbintohexstr(convert(binary(16),J.job_id )) + '%'

     order by JobName

    It takes a while, but have a look.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • That works exactly like I wanted. It is a bit slow (it took 18 seconds to complete with only one job running), but it will work.

    Thanks for your help!

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply