Search code examples
sqlsql-serverssissql-server-2017

SSIS : Execute SQL Server Agent Job Task run Asychronously or Synchronously


Do "Execute SQL Server Agent Job Task" in SSIS, run synchronously or asynchronously in SSIS?

(a) Synchronous: Meaning does the job trigger and Wait till completion, then go to the next step,

(b) Asynchronous: Or does it just trigger it, and immediately go to next step?

7


Solution

  • It runs asynchronously - fire and forget

    Here's how you can easily verify this behavior.

    SQL Agent Job

    This job executes a TSQL Command called WAITFOR. I specify wait for 15 seconds. Run the job by itself and in 16 seconds or less, you should have an entry in the job history report showing the job ran successfully.

    USE msdb;
    GO
    
    DECLARE @jobId binary(16);
    
    EXEC msdb.dbo.sp_add_job
        @job_name = N'Run for 15 seconds'
    ,   @enabled = 1
    ,   @notify_level_eventlog = 0
    ,   @notify_level_email = 2
    ,   @notify_level_page = 2
    ,   @delete_level = 0
    ,   @category_name = N'[Uncategorized (Local)]'
    ,   @owner_login_name = N'sa'
    ,   @job_id = @jobId OUTPUT;
    
    SELECT
        @jobId;
    GO
    
    EXEC msdb.dbo.sp_add_jobserver
        @job_name = N'Run for 15 seconds'
    ,   @server_name = N'BUCKLAND\DEV2017';
    GO
    
    USE msdb;
    GO
    
    EXEC msdb.dbo.sp_add_jobstep
        @job_name = N'Run for 15 seconds'
    ,   @step_name = N'Wait for it'
    ,   @step_id = 1
    ,   @cmdexec_success_code = 0
    ,   @on_success_action = 1
    ,   @on_fail_action = 2
    ,   @retry_attempts = 0
    ,   @retry_interval = 0
    ,   @os_run_priority = 0
    ,   @subsystem = N'TSQL'
    ,   @command = N'WAITFOR DELAY ''00:00:15'';'
    ,   @database_name = N'master'
    ,   @flags = 0;
    GO
    
    USE msdb;
    GO
    
    EXEC msdb.dbo.sp_update_job
        @job_name = N'Run for 15 seconds'
    ,   @enabled = 1
    ,   @start_step_id = 1
    ,   @notify_level_eventlog = 0
    ,   @notify_level_email = 2
    ,   @notify_level_page = 2
    ,   @delete_level = 0
    ,   @description = N''
    ,   @category_name = N'[Uncategorized (Local)]'
    ,   @owner_login_name = N'sa'
    ,   @notify_email_operator_name = N''
    ,   @notify_page_operator_name = N'';
    GO
    

    SSIS package

    I created an SSIS package, added a task of type Execute SQL Server Agent Job Task and pointed it at the server and job. I ran it and whoosh the package completed successfully in under two seconds.

    SSIS Results Tab indicating 2 second execution duration