Wednesday, January 12, 2022

SQL Server. Kill runaway jobs

 select sja.*

FROM msdb.dbo.sysjobactivity AS sja

INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id

WHERE sja.start_execution_date IS NOT NULL

AND sja.stop_execution_date IS NULL

and sj.name = 'FIN_ICRP_BOM_Generate_QCHART'


kill 35


USE msdb


GO


EXEC dbo.sp_stop_job


N'FIN_ICRP_BOM_Generate_QCHART'


--use this

DECLARE @job_name VARCHAR(2000) = 'FIN_ICRP_BOM_Generate_QCHART'; 

 

DECLARE @job_id uniqueidentifier =

    (SELECT job_id

    FROM msdb.dbo.sysjobs

    WHERE NAME LIKE @job_name); 

 

UPDATE msdb.dbo.sysjobactivity

SET stop_execution_date = GETDATE()

WHERE job_id = @job_id

AND start_execution_date =

    (SELECT MAX(start_execution_date)

    FROM msdb.dbo.sysjobactivity

    WHERE job_id = @job_id

AND run_Requested_date IS NOT NULL 

AND stop_execution_date IS NULL);

 

EXEC sp_stop_job @job_id = @job_id;