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 8467823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:45:23+00:00 2026-06-10T15:45:23+00:00

Need to have a stored procedure that calls a SQL Server Agent Job and

  • 0

Need to have a stored procedure that calls a SQL Server Agent Job and returns whether or not the job ran successfully or not.

So far I have

CREATE PROCEDURE MonthlyData
AS
EXEC msdb.dbo.sp_start_job N'MonthlyData'

WAITFOR DELAY '000:04:00'

EXEC msdb.dbo.sp_help_jobhistory @job_name = 'MonthlyData'
GO

Which starts the job, whats the best way to get back if the job ran successfully or not?

Ok made an edit and used WAITFOR DELAY as the job normally runs between 3-4 mins never longer than 4. Does the job but is there a more efficient way to do it?

  • 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-06-10T15:45:25+00:00Added an answer on June 10, 2026 at 3:45 pm

    You can run the query:

    EXEC msdb.dbo.sp_help_jobhistory 
        @job_name = N'MonthlyData'
    

    It’ll return a column run_status. Statuses are:

     0 - Failed
     1 - Succeeded
     2 - Retry
     3 - Canceled         
    

    More info on MSDN

    EDIT: You might want to to poll your job and make sure it’s executed. You can get this information from sp_help_job procedure. When this procedure returns status of 4 it means the job is idle.
    Then it’s safe to check for it’s run status.

    You can poll using following code:

    DECLARE @job_status INT
    SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''NightlyBackups''')
    
    WHILE @job_status <> 4
    BEGIN
        WAITFOR DELAY '00:00:03'
        SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''NightlyBackups''')
    END
    
    EXEC msdb.dbo.sp_help_jobhistory 
        @job_name = N'NightlyBackups' ;
    GO
    

    This code will check for the status, wait for 3 seconds and try again. Once we get status of 4 we know the job is done and it’s safe to check for the job history.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing stored procedure in SQL Server that I need to call
In SQL Server, I have a stored procedure that accepts a date parameter. That
I have a dynamic SQL statement I've created in a stored procedure. I need
i have stored the txt file to sql server database . i need to
I have a stored procedure that returns a valueI call it from other stored
I have stored procedure in my database and i need to look up a
I have a simple Java class that I need to serialize to be stored
I have a SQL Server table full of orders that my program needs to
I have a ms sql server database with a growing number of stored procedures
How do you unit test your code that utilizes stored procedure calls? In my

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.