Hey guys,
Good afternoon.
After a while without posting, today I will demonstrate in this post how to identify running jobs via Query in SQL Server. With this query, it is possible to identify the name and id of the job, the step that the job is executing, which query it is running now and for how long.
How to identify running jobs via Query
During everyday life, we can always monitor the queries that are running in our environment, mainly using the great procedure sp_whoisactive. However, even with this excellent tool, it is not so trivial to find out via Query if a job has been running, for how long, in which step, etc.
With this in mind, I will demonstrate a query (I created it as a view for easy consultation) that helps us with this task:
SELECT
F.session_id,
A.job_id,
C.name AS job_name,
F.login_name,
F.[host_name],
F.[program_name],
A.start_execution_date,
CONVERT(VARCHAR, CONVERT(VARCHAR, DATEADD(ms, ( DATEDIFF(SECOND, A.start_execution_date, GETDATE()) % 86400 ) * 1000, 0), 114)) AS time_elapsed,
ISNULL(A.last_executed_step_id, 0) + 1 AS current_executed_step_id,
D.step_name,
H.[text]
FROM
msdb.dbo.sysjobactivity A WITH(NOLOCK)
LEFT JOIN msdb.dbo.sysjobhistory B WITH(NOLOCK) ON A.job_history_id = B.instance_id
JOIN msdb.dbo.sysjobs C WITH(NOLOCK) ON A.job_id = C.job_id
JOIN msdb.dbo.sysjobsteps D WITH(NOLOCK) ON A.job_id = D.job_id AND ISNULL(A.last_executed_step_id, 0) + 1 = D.step_id
JOIN (
SELECT CAST(CONVERT( BINARY(16), SUBSTRING([program_name], 30, 34), 1) AS UNIQUEIDENTIFIER) AS job_id, MAX(login_time) login_time
FROM sys.dm_exec_sessions WITH(NOLOCK)
WHERE [program_name] LIKE 'SQLAgent - TSQL JobStep (Job % : Step %)'
GROUP BY CAST(CONVERT( BINARY(16), SUBSTRING([program_name], 30, 34), 1) AS UNIQUEIDENTIFIER)
) E ON C.job_id = E.job_id
LEFT JOIN sys.dm_exec_sessions F WITH(NOLOCK) ON E.job_id = CAST(TRY_CONVERT( BINARY(16), SUBSTRING(F.[program_name], 30, 34), 1) AS UNIQUEIDENTIFIER) AND E.login_time = F.login_time
LEFT JOIN sys.dm_exec_connections G WITH(NOLOCK) ON F.session_id = G.session_id
OUTER APPLY sys.dm_exec_sql_text(most_recent_sql_handle) H
WHERE
A.session_id = ( SELECT TOP 1 session_id FROM msdb.dbo.syssessions WITH(NOLOCK) ORDER BY agent_start_date DESC )
AND A.start_execution_date IS NOT NULL
AND A.stop_execution_date IS NULL
If you are using a version of SQL Server prior to version 2012, this query above will return an error message due to TRY_CONVERT. To do this, I have another query, which uses the BINARY_CHECKSUM function to validate whether the string is in BINARY format:
SELECT
F.session_id,
A.job_id,
C.name AS job_name,
F.login_name,
F.[host_name],
F.[program_name],
A.start_execution_date,
CONVERT(VARCHAR, CONVERT(VARCHAR, DATEADD(ms, ( DATEDIFF(SECOND, A.start_execution_date, GETDATE()) % 86400 ) * 1000, 0), 114)) AS time_elapsed,
ISNULL(A.last_executed_step_id, 0) + 1 AS current_executed_step_id,
D.step_name,
H.[text]
FROM
msdb.dbo.sysjobactivity A WITH(NOLOCK)
LEFT JOIN msdb.dbo.sysjobhistory B WITH(NOLOCK) ON A.job_history_id = B.instance_id
JOIN msdb.dbo.sysjobs C WITH(NOLOCK) ON A.job_id = C.job_id
JOIN msdb.dbo.sysjobsteps D WITH(NOLOCK) ON A.job_id = D.job_id AND ISNULL(A.last_executed_step_id, 0) + 1 = D.step_id
JOIN (
SELECT CAST(CONVERT( BINARY(16), SUBSTRING([program_name], 30, 34), 1) AS UNIQUEIDENTIFIER) AS job_id, MAX(login_time) login_time
FROM sys.dm_exec_sessions WITH(NOLOCK)
WHERE [program_name] LIKE 'SQLAgent - TSQL JobStep (Job % : Step %)'
GROUP BY CAST(CONVERT( BINARY(16), SUBSTRING([program_name], 30, 34), 1) AS UNIQUEIDENTIFIER)
) E ON C.job_id = E.job_id
LEFT JOIN sys.dm_exec_sessions F WITH(NOLOCK) ON E.job_id = (CASE WHEN BINARY_CHECKSUM(SUBSTRING(F.[program_name], 30, 34)) > 0 THEN CAST(TRY_CONVERT( BINARY(16), SUBSTRING(F.[program_name], 30, 34), 1) AS UNIQUEIDENTIFIER) ELSE NULL END) AND E.login_time = F.login_time
LEFT JOIN sys.dm_exec_connections G WITH(NOLOCK) ON F.session_id = G.session_id
OUTER APPLY sys.dm_exec_sql_text(most_recent_sql_handle) H
WHERE
A.session_id = ( SELECT TOP 1 session_id FROM msdb.dbo.syssessions WITH(NOLOCK) ORDER BY agent_start_date DESC )
AND A.start_execution_date IS NOT NULL
AND A.stop_execution_date IS NULL
Or there is a slightly simpler query, which does not return the job session_id, hostname, user, program or the query being executed, but returns all information related to the Jobs that are being executed in the instance:
SELECT
A.job_id,
C.name AS job_name,
E.name AS job_category,
C.[enabled],
C.[description],
A.start_execution_date,
A.last_executed_step_date,
A.next_scheduled_run_date,
CONVERT(VARCHAR, CONVERT(VARCHAR, DATEADD(SECOND, ( DATEDIFF(SECOND, A.start_execution_date, GETDATE()) % 86400 ), 0), 114)) AS time_elapsed,
ISNULL(A.last_executed_step_id, 0) + 1 AS current_executed_step_id,
D.step_name
FROM
msdb.dbo.sysjobactivity A WITH(NOLOCK)
LEFT JOIN msdb.dbo.sysjobhistory B WITH(NOLOCK) ON A.job_history_id = B.instance_id
JOIN msdb.dbo.sysjobs C WITH(NOLOCK) ON A.job_id = C.job_id
JOIN msdb.dbo.sysjobsteps D WITH(NOLOCK) ON A.job_id = D.job_id AND ISNULL(A.last_executed_step_id, 0) + 1 = D.step_id
JOIN msdb.dbo.syscategories E WITH(NOLOCK) ON C.category_id = E.category_id
WHERE
A.session_id = ( SELECT TOP 1 session_id FROM msdb.dbo.syssessions WITH(NOLOCK) ORDER BY agent_start_date DESC )
AND A.start_execution_date IS NOT NULL
AND A.stop_execution_date IS NULL
Another way to identify running jobs is by using the undocumented system procedure, xp_sqlagent_enum_jobs. This procedure aims to list all jobs in the instance, informing the date and time of the last execution, date and time of the next execution, binary flag if the job is currently running (running), the number of the step being executed (Current_Step), the number of Retry attempts and the current state of the execution.
EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''
By working a little with this sp, we can obtain a more effective result for our needs:
IF (OBJECT_ID('tempdb..#Retorno') IS NOT NULL) DROP TABLE #Retorno
CREATE TABLE #Retorno (
Job_Id UNIQUEIDENTIFIER,
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 VARCHAR(MAX),
Running BIT,
Current_Step INT,
Current_Retry_Attempt INT,
[State] SMALLINT
)
INSERT INTO #Retorno
EXEC master.dbo.xp_sqlagent_enum_jobs 1, ''
IF (OBJECT_ID('tempdb..#Resultado') IS NOT NULL) DROP TABLE #Resultado
SELECT
Job_Id,
(CASE WHEN Last_Run_Date > 0 THEN msdb.dbo.agent_datetime(Last_Run_Date, Last_Run_Time) END) AS Dt_Ultima_Execucao,
(CASE WHEN Next_Run_Date > 0 THEN msdb.dbo.agent_datetime(Next_Run_Date, Next_Run_Time) END) AS Dt_Proxima_Execucao,
Next_Run_Schedule_ID,
Requested_To_Run,
Request_Source,
Request_Source_ID,
Running,
Current_Step,
Current_Retry_Attempt,
State
INTO
#Resultado
FROM
#Retorno
WHERE
Running = 1 -- Em Execução
SELECT * FROM #Resultado
That's it, folks!
Until later!



Comentários (0)
Carregando comentários…