Create And Schedule A SQLAgent Job From TSQL

  • I'd like to build a TSQL stored procedure that sets up a SQL Agent job, creating the steps, scheduling etc.

    Has anyone done this and knows of any 'gotchas' I need to prepare for. Even better could anyone help with a ready-rolled sp. (OK I know I'm lazy, anything to save time).

    We use SQLServer version 7 in production (we're still UATing our apps on 2000)

    Much gratitude in advance

    I have found it is possible to please all of the people all of the time if you do exactly what they want. Harold Macmillan 1961


    I have found it is possible to please all of the people all of the time if you do exactly what they want. Harold Macmillan 1961

  • Look at the system stored procedures:

    • sp_add_job
    • sp_add_jobstep
    • sp_add_jobschedule

    K. Brian Kelley

    http://www.truthsolutions.com/

    Author: Start to Finish Guide to SQL Server Performance Monitoring

    http://www.netimpress.com/shop/product.asp?ProductID=NI-SQL1

    K. Brian Kelley
    @kbriankelley

  • Thanks Brian

    I'd sussed those procs, I was really after any known Gotcha's.

    Well let's be brutally honest I was hoping that someone had a ready-rolled proc I could clone, why re-invent the wheel? (Or rather why think when someone else can do it?)

    We've built the proc now, basically it cycles round a table which contains one record for each line of the script and invoke the add step etc.

    I have found it is possible to please all of the people all of the time if you do exactly what they want. Harold Macmillan 1961


    I have found it is possible to please all of the people all of the time if you do exactly what they want. Harold Macmillan 1961

  • I would create the jobs with EM, script it and then create a master sp with all the job SPs included. You should change all the variables that you want to specify to your sp.

  • Hi ,

    I tried to use

  • sp_add_job
  • sp_add_jobstep
  • sp_add_jobschedule
  • But got an error saying the procs could not be found.

    Anybody got any clues? I am using SqlServer 2000

    Bastiaan Oelrich

     

  • I found the solution to the problem above myself. I thought the system stored procedures would be located in master db, but these are located in msdb db

     

    Bastiaan

  • Viewing 6 posts - 1 through 5 (of 5 total)

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