Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6177737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:19:42+00:00 2026-05-24T00:19:42+00:00

I am using below script to get the Job_state for Agent Job. It returns

  • 0

I am using below script to get the Job_state for Agent Job.

It returns 1 as job_state when job is running => which is fine.

But for all other cases (failed / success / cancelled) it returns job_status 4 (which means its idle)

How can I get exact job_state so that I can determine whether it is failed / success or cancelled.

My script so far:

DECLARE @xp_results TABLE
(
  job_id UNIQUEIDENTIFIER NOT NULL ,
  last_run_date INT NOT NULL ,
  last_run_time INT NOT NULL ,
  next_run_date INT NOT NULL ,
  next_run_time INT NOT NULL ,
  next_run_schedule_id INT NOT NULL ,
  requested_to_run INT NOT NULL ,
  request_source INT NOT NULL ,
  request_source_id SYSNAME COLLATE database_default NULL ,
  running INT NOT NULL ,
  current_step INT NOT NULL ,
  current_retry_attempt INT NOT NULL ,
  job_state INT NOT NULL
)      
SET NOCOUNT ON
INSERT  INTO @xp_results
    EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''

DECLARE @jobs TABLE
(
  rownum INT IDENTITY(1, 1)
             PRIMARY KEY
             NOT NULL ,
  job_id UNIQUEIDENTIFIER NOT NULL
)

SELECT  name,job_state
FROM    @xp_results rj
    INNER JOIN msdb.dbo.sysjobs sj ON sj.job_id = rj.job_id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:19:43+00:00Added an answer on May 24, 2026 at 12:19 am

    I got this resolved as below. There should be other good way to do it.

    DECLARE @xp_results TABLE
    (
      job_id UNIQUEIDENTIFIER NOT NULL ,
      last_run_date INT NOT NULL ,
      last_run_time INT NOT NULL ,
      next_run_date INT NOT NULL ,
      next_run_time INT NOT NULL ,
      next_run_schedule_id INT NOT NULL ,
      requested_to_run INT NOT NULL ,
      request_source INT NOT NULL ,
      request_source_id SYSNAME COLLATE database_default NULL ,
      running INT NOT NULL ,
      current_step INT NOT NULL ,
      current_retry_attempt INT NOT NULL ,
      job_state INT NOT NULL
    )      
    SET NOCOUNT ON
    INSERT  INTO @xp_results
        EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''
    
    DECLARE @jobs TABLE
    (
      rownum INT IDENTITY(1, 1)
                 PRIMARY KEY
                 NOT NULL ,
      job_id UNIQUEIDENTIFIER NOT NULL
    )
    DROP TABLE #temp    
    exec sp_serveroption 'hts0519', 'data access', 'true'
    select * 
    INTO #temp
    FROM OPENQUERY( hts0519, ' EXEC msdb.dbo.sp_help_job')
    
    SELECT  sj.name,
    
        CASE job_state WHEN 4 THEN 'IDLE' ELSE 'RUNNING' END AS CURRENT_STATUS, 
        CASE tmp.last_run_outcome
            WHEN 0 THEN 'FAILED'
            WHEN 1 THEN 'SUCCEEDED'
            WHEN 3 THEN 'CANCELED'
            WHEN 5 THEN 'UNKNOWN'   
        END  AS LAST_OUTCOME
     FROM    @xp_results rj
        INNER JOIN msdb.dbo.sysjobs sj ON sj.job_id = rj.job_id
        INNER JOIN #temp tmp ON sj.name = tmp.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below mentioned script to get href of new opened window, but
Trying to: get all users by using the query in the below script into
I have tested the below script on a demo page which is not using
I have a small script which im using to test PHP mail(), as below:
In Graph API › Photo, I'm using below script to get Photo attributes $response
I am using the below script line to get the IP information of system
I am using below jquery to get selected row id <script src=../Scripts/jquery-1.4.1.min.js type=text/javascript></script> <script
I want to enable auto update script in flex3. i am using below code
The script below worked on my Mac OS X. I'm now using Ubuntu OS,
Currently i'm using below code which works well. $(#topperAtBaseLevel:visible, #lowerAtBaseLevel:visible, #midAtBaseLevel).hide(); any optimised code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.