Skip to content
Advertisement

SQL agent job creation error: Job ‘XXX’ is already targeted at server ‘YYY’

I am trying to create and SQL agent job on SQL Server 2016. Here is the use of sp_add_job.

EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'TEST_RUN_A_JOB_STEP',
    @enabled=1,
    @notify_level_eventlog=0,
    @notify_level_email=0,
    @notify_level_netsend=0,
    @notify_level_page=0,
    @delete_level=0,
    @description=N'TEST_RUN_A_JOB_STEP',
    @category_name=N'EDW',
    @owner_login_name=N'JobCreator', @job_id = @jobId OUTPUT;

The error message appears:

Msg 14269, Level 16, State 1, Procedure sp_add_jobserver, Line 101 [Batch Start Line 2]
Job ‘TEST_RUN_A_JOB_STEP’ is already targeted at server ‘EDW222’.

I have done significant searching with no results. There is no job with such a name. SELECT COUNT(*) FROM sysjobs WHERE name LIKE 'TEST_RUN%'; returns zero (0).

The job that does not exist cannot be deleted with sp_delete_job.

Why can this job not be created?

Revision 1

Here is the entire script if that will help.

USE [msdb]
GO

BEGIN TRANSACTION;
DECLARE @ReturnCode INT;
SELECT @ReturnCode = 0;

IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'EDW' AND category_class=1)
BEGIN
    EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'EDW';
    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback;
END

IF EXISTS (SELECT 1 FROM SYSJOBS WHERE name = 'TEST_RUN_A_JOB_STEP')
    EXEC msdb.dbo.sp_delete_job @job_name=N'TEST_RUN_A_JOB_STEP', @delete_unused_schedule=1;

DECLARE @jobId BINARY(16);

SET @jobId = NULL;
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'TEST_RUN_A_JOB_STEP',
        @enabled=1,
        @notify_level_eventlog=0,
        @notify_level_email=0,
        @notify_level_netsend=0,
        @notify_level_page=0,
        @delete_level=0,
        @description=N'TEST_RUN_A_JOB_STEP',
        @category_name=N'EDW',
        @owner_login_name=N'JobCreator', @job_id = @jobId OUTPUT;
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback;

-- Create a "step 1" that does nothing in case the job is run manually
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'RUN_A_JOB_STEP_NULL',
        @step_id=1,
        @cmdexec_success_code=0,
        @on_success_action=1,
        @on_success_step_id=0,
        @on_fail_action=2,
        @on_fail_step_id=0,
        @retry_attempts=0,
        @retry_interval=0,
        @os_run_priority=0, @subsystem=N'CmdExec',
        @command=N'ECHO here && EXIT 0',
        @database_name=N'master',
        @flags=0,
        @proxy_name=N'EDW_Proxy'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- An SSIS package step
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'RUN_A_COMMAND_PAUL',
        @step_id=2,
        @cmdexec_success_code=0,
        @on_success_action=1,
        @on_success_step_id=0,
        @on_fail_action=2,
        @on_fail_step_id=0,
        @retry_attempts=0,
        @retry_interval=0,
        @os_run_priority=0, @subsystem=N'SSIS',
        @command=N'/FILE "E:SSISEDWAnalysisRunOsCommand.dtsx" /CHECKPOINTING OFF /REPORTING E',
        @database_name=N'master',
        @flags=0,
        @proxy_name=N'EDW_Proxy'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- Commit everything and get out
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

Advertisement

Answer

Alternatively, you can use the SSMS interface to setup the jobs details and then click to generate SQL Script option.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement