Showing posts with label Is my job running now. Show all posts
Showing posts with label Is my job running now. Show all posts

Thursday, March 14, 2019

Check Job Status [Running/Not] using tSQL, SQL Server

Sometimes SQL Server job history doesn't show that a job is running currently or not.
For what we need to check this using SQL.

Check my other post to "View Job History using tSQL, SQL Server".

I always use below to check "Is my job running now?"


--CREATE PROCEDURE sp_IsJobRunning
--@JobName as varchar(100) = Null
--AS
--BEGIN
declare @JobName as varchar(100) = Null
CREATE TABLE #Job_sazam (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, -- BOOL
request_source INT NOT NULL,
request_source_id sysname COLLATE database_default NULL,
running INT NOT NULL, -- BOOL
current_step INT NOT NULL,
current_retry_attempt INT NOT NULL,
job_state INT NOT NULL)
-- Get Job Information
-- you need "sysadmin" role or sql_agent related role access to execute  xp_sqlagent_enum_jobs.
INSERT INTO #Job_sazam
EXEC master.dbo.xp_sqlagent_enum_jobs 1,dbo

SELECT Running, sj.name
FROM #Job_sazam J INNER JOIN msdb.dbo.sysjobs SJ ON
J.job_ID = SJ.job_ID
--WHERE [name] = @JobName
order by 1 desc, 2 asc
drop table #Job_sazam
--END