Sql-server – Start failed for Job in SQL Server Agent

errorsjobssql-server-agentssmsstored-procedures

I am trying to schedule a job which executes stored procedure but it is failing.

DECLARE @return_value int
EXEC    @return_value = [dbo].[sp_WarehouseAttendance_New]
SELECT  'Return Value' = @return_value

If I try to run it manually, I am getting this error.

Manually Execute Job Error

At the moment I am trying to run EXECUTE storedproc but it takes long time to complete.

I checked Services.msc and it shows as below.

Services.msc

Kindly let me know how to avoid this.

Thanks,
Ar

Best Answer

Looks like the problem is exactly in the description - the job is already running.

Try stopping the job first, then re-running it. Either with the GUI, or

USE msdb 
GO

EXEC dbo.sp_stop_job
    N'Job Name' 
GO

Other things to check: What is the job doing? Can you do that (run the script, for example) directly (not using Agent)?

Has the job got elements (e.g. a script) which can never actually finish, therefore the job is never going to stop (and therefore will not start again on schedule).

It's a bit hacky, but you can schedule a job-killing job to stop any jobs that are over-running (although it's usually better to try fix your jobs so they finish properly).

This script from SQL Server Central provides some handy info about currently running jobs:

SELECT
    ja.job_id,
    j.name AS job_name,
    ja.start_execution_date,      
    ISNULL(last_executed_step_id,0)+1 AS current_executed_step_id,
    Js.step_name
FROM msdb.dbo.sysjobactivity ja 
LEFT JOIN msdb.dbo.sysjobhistory jh 
    ON ja.job_history_id = jh.instance_id
JOIN msdb.dbo.sysjobs j 
ON ja.job_id = j.job_id
JOIN msdb.dbo.sysjobsteps js
    ON ja.job_id = js.job_id
    AND ISNULL(ja.last_executed_step_id,0)+1 = js.step_id
WHERE ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)
AND start_execution_date is not null
AND stop_execution_date is null;

That should help you sort out your problem, if not, post some more details about what exactly the job is doing.

Edit- Code to check SQL server agent is running (run from Management studio). DECLARE @agent NVARCHAR(512);

SELECT @agent = COALESCE(N'SQLAgent$' + CONVERT(SYSNAME, SERVERPROPERTY('InstanceName')), 
  N'SQLServerAgent');

EXEC master.dbo.xp_servicecontrol 'QueryState', @agent;

You can manage Agent from SQL Server Configuration Manager.

Ian