0

Is there a good way to query the job start time inside of a given sql agent job step (TSQL)? This will help with step 3 in the following scenario.

Job Foo (Run Daily @ 11:30pm)
Step1 - StartTime = Saturday 11:30pm TSQL - (15 minute duration)
Step2 - StartTime = Saturday 11:45pm TSQL - (30 minute duration)
Step3 - StartTime = Sunday 12:15am TSQL - Exit early / don't run unless Foo job was started on a Saturday.

2 Answers 2

0

Here's a script to build a view that shows current job's run status and current step.

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
CREATE OR ALTER   PROCEDURE ETLSteps.ListJobsCurrentRunningStatus 
AS
BEGIN
    SET NOCOUNT ON
    SET ANSI_WARNINGS OFF
    DECLARE @currentjobstatus TABLE([Job ID] binary(16), [Last Run Date] int, [Last Run Time] int, [Next Run Date] int, [Next Run Time] int, [Next Run Schedule ID] int, [Requested To Run] int, [Request Source] int, [Request Source ID] nvarchar(66), [Running] int, [Current Step] int, [Current Retry Attempt] int, [State] int )
    INSERT INTO @currentjobstatus
    EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''
    SELECT job_id_bin = [Job ID], job_is_running = Running, current_step_id = [Current Step], current_step_retry_attempt_no = [Current Retry Attempt], current_run_status = State FROM @currentjobstatus
    RETURN 0
    DECLARE @rtn int
    EXEC @rtn =  ETLSteps.ListJobsCurrentRunningStatus 
END
GO

EXEC master.dbo.sp_addlinkedserver @server = N'.\instance', @srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'.\instance',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'rpc', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'rpc out', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'collation name', @optvalue=NULL
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'use remote collation', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'.\instance', @optname=N'remote proc transaction promotion', @optvalue=N'true'
GO

CREATE OR ALTER VIEW ETLSteps.AgentJobsCurrentlyRunning AS
SELECT 
    job_id = CAST(job_id_bin AS UNIQUEIDENTIFIER)
,   job_id_bin
,   job_is_running 
,   current_step_id 
,   current_step_retry_attempt_no
,   current_run_status 
FROM OPENQUERY([.\instance], 'DBABnch_Local.ETLSteps.ListJobsCurrentRunningStatus') query
GO
SELECT * FROM ETLSteps.AgentJobsCurrentlyRunning
1

There is a lot of information about the jobs and SQL Server Agent in msdb.

You can check it out below, I added a few columns that help you in your thoughts. Maybe you want to check If this job success or not (in the message column).

SELECT  JobName = sj.name,
        StepName = jh.step_name,
        RunDateTime = msdb.dbo.agent_datetime(run_date, run_time),
        EndDateTime = DATEADD(SECOND, run_duration / 10000 * 3600 + run_duration % 10000 / 100 * 60 + run_duration % 100, msdb.dbo.agent_datetime(jh.run_date, jh.run_time)),
        jh.message
FROM msdb.dbo.sysjobs sj
INNER JOIN msdb.dbo.sysjobhistory jh ON sj.job_id = jh.job_id
where step_id=0

For the comment: There is no clearly answered yet about that. So, you should try it.

Microsoft says;

In most cases the data is updated only after the job step completes and the table typically contains no records for job steps that are currently in progress, but in some cases underlying processes do provide information about in progress job steps.

3
  • Will the sysjobhistory be populated for the current job even though the job is currently running and hasn't finished? Commented Dec 10, 2020 at 3:08
  • I updated my answer, you may check it out. Commented Dec 10, 2020 at 8:24
  • It does not appear that msdb.dbo.sysjobhistory is updated while a job is still running, I just checked with a long running job on our production system, and the latest record in sysjobhistory is from yesterday. You may have better luck with run_requested_date FROM msdb.dbo.sysjobactivity.
    – BCM
    Commented Dec 10, 2020 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.