Search code examples
sqlt-sqlsql-server-2012ssmssql-server-agent

SQL Server 2012 job scheduling


I have a situation where I have 1 package (Package A) that take about 3 min to complete and another package (Package B) that takes about 4 min to complete. I need package A to run every 5 min via a job and package B to run every 15 min. However package B can only run after package A has been completed.

Example

5- Package A runs

10- Package A runs

15- Package A runs and then Package B

20- Package A runs

25- Package A runs

30- Package A runs and then Package B

Is there any way to schedule jobs to meet the requirements above?

Really appreciate your help. Thanks in advance!


Solution

  • for package B i would just put in logic to check if package A has stopped running

    the wait script would be

    WHILE EXISTS (
    SELECT
        *
    FROM 
        msdb.dbo.sysjobs_view job
    JOIN
        msdb.dbo.sysjobactivity activity
    ON 
        job.job_id = activity.job_id
    JOIN
        msdb.dbo.syssessions sess
    ON
        sess.session_id = activity.session_id
    JOIN
    (
        SELECT
            MAX( agent_start_date ) AS max_agent_start_date
        FROM
            msdb.dbo.syssessions
    ) sess_max
    ON
        sess.agent_start_date = sess_max.max_agent_start_date
    WHERE 
        run_requested_date IS NOT NULL AND stop_execution_date IS NULL
        AND job.name = 'package A') 
    BEGIN 
    WAITFOR DELAY '00:00:30'; 
    END 
    

    This would check every 30 seconds if package A has stopped running.

    Alternatively, you create a table that would keep track of job status and have A write to it to keep track of the status.