How to run multiple dependent SQL Jobs with OSQL?

  • I want to execute multiple SQL Server scheduled jobs using OSQL, but I only want to execute a job if the previous job completed.  Is this possible?  So far I am unable to prevent all jobs from starting before any have completed.

    OSQL -Sservername -E -b -Q"usp_start_job 'JobNumber1'" -o usp_start_job.out

    IF ERRORLEVEL 1 GOTO ERROR

    OSQL -Sservername -E -b -Q"usp_start_job 'JobNumber2'" -o usp_start_job.out

    IF ERRORLEVEL 1 GOTO ERROR

    OSQL -Sservername -E -b -Q"usp_start_job 'JobNumber3'" -o usp_start_job.out

    IF ERRORLEVEL 1 GOTO ERROR

    GOTO EXIT

    :ERROR

    ECHO *** ERROR *** Check Log File

    :EXIT

    *** JOB COMPLETED ***

    Dave

  • you could offcourse use an alert in JobNumber1 final success step, wich launches JobNumber2, ... 

    If this job-launching is depending on who/what launched the current job, you could have a parametertable to couver this issue.

     

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • I suggested doing just that, but since the scheduled jobs are created by a third-party software package, they don't want the scheduled jobs altered since the changes may disappear during software upgrades.  I'm either going to propose checking sysjobhistory within a loop (using WAITFOR) or suggest they execute the stored procedures directly since all the scheduled jobs do is start one stored procedure each.  This is probably the simplest solution.

    Thanks,  Dave

  • I was going to propose to simply add a final step to each job with sp_start_job "nextJob". But if you can't modify it then polling is the only thing I can think of but instead of using sysjobhistory I would go for sp_help_job

     

    Cheers,

     


    * Noel

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply