Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL 102 – Jobs

Posted on November 15, 2006March 1, 2011 by slowder

Now that you’re familiar with stored procedures, it’s time to introduce you to jobs. Jobs are like stored procedures, in that they are a set of steps performed sequentially. Jobs can run T-SQL commands, Integration Services Packages, Analysis Services commands, replication tasks, and even command prompt applications.

And what’s the best part?

These steps are run by the SQL Server Agent and they can be scheduled. That means no more waking up in the middle of the night to run a procedure to index a table, or check for some condition.

You can now make the server work a little harder for you.

To create or manage jobs, you’ll need to have permission, you’ll either need access to the Agent role, or administrator role. You could create your job via SQL Server Management Studio, but you’d then have to script out that change to T-SQL, so you can follow rule number 4. So I’ll skip the graphical method, and skip straight to creating the T-SQL code.

First you have to Create the job.

DECLARE @jobID BINARY(16)

EXEC msdb.dbo.sp_add_job
    @job_name=N'Job Name'
  , @job_id = @jobID OUTPUT
PRINT @jobID

There are other parameters that sp_add_job will take, if you want a full list of that, check out Microsoft’s Books online.

Once you have created the job we’ll move on to adding steps, but I would like to note here you can modify the job options at any time by using sp_update_job. Just keep in mind you’ll need the job_id of the job you wish to change.

Now, let’s add a step to the job we’ve created.

DECLARE @jobID BINARY(16)
SELECT
  @jobID = job_id
FROM msdb..sysjobs
WHERE
  name = 'Job Name'

EXEC msdb.dbo.sp_add_jobstep
    @job_id=@jobId
  , @step_name=N'Do SQL Stuff'
  , @step_id=1
  , @subsystem=N'TSQL'
  , @command=N'SELECT ''Hello, my name is Inigo Montoya, you killed my father...Prepare to die!'''
  , @database_name=N'DB_Name'
  , @flags=0

The SELECT query sets your job ID value for you. Then you’ll need to provide the step name, and the T-SQL query as well as the database name. There are more options available to you. If you need to reference those, please see the Microsoft Books Online.

I’d also like to point out the subsystem step… this is currently set to T-SQL, but you can change this to run other things, like SSIS Packages!

Another note: if you need to remove a step, please see the reference for sp_delete_jobstep.

Now, let’s set the schedule.

DECLARE @jobID BINARY(16)
SELECT
  @jobID = job_id
FROM msdb..sysjobs
WHERE
  name = 'Job Name'

EXEC msdb.dbo.sp_add_jobschedule
    @job_id=@jobId
  , @name=N'Mon-Fri 6:00AM to 7:00PM, every hour'
  , @freq_type=8
  , @freq_interval=62
  , @freq_subday_type=8
  , @freq_subday_interval=1
  , @freq_relative_interval=0
  , @freq_recurrence_factor=1
  , @active_start_date=20100302
  , @active_end_date=99991231
  , @active_start_time=60000
  , @active_end_time=190000

Again, you’ll get the job ID from the SELECT statement, and you’ll have to provide a schedule name. That’s easy part, the rest is a little odd at first.
You have to choose a frequency type. Choose one of the following:

Value Description
1 Once
4 Daily
8 Weekly
16 Monthly
32 Monthly, relative to freq_interval
64 Run when SQLServerAgent service starts
128 Run when the computer is idle

You’ll also have to choose a frequency interval, this is based on your frequency type. Choose one of the following:

Value of freq_type Effect on freq_interval
1 (once) freq_interval is unused.
4 (daily) Every freq_interval days.
8 (weekly) freq_interval is one or more of the following (combined with an OR logical operator):

1 = Sunday

2 = Monday

4 = Tuesday

8 = Wednesday

16 = Thursday

32 = Friday

64 = Saturday

16 (monthly) On the freq_interval day of the month.
32 (monthly relative) freq_interval is one of the following:

1 = Sunday

2 = Monday

3 = Tuesday

4 = Wednesday

5 = Thursday

6 = Friday

7 = Saturday

8 = Day

9 = Weekday

10 = Weekend day

64 (when SQLServerAgent service starts) freq_interval is unused.
128 freq_interval is unused.

Then you’ll have to enter a frequency subday type. Again, just choose one of the following:

Value Description (unit)
0x1 At the specified time
0x4 Minutes
0x8 Hours

Next, you’ll have to give a frequency subday interval, 1 means ignore this parameter. You’ll also need a frequency relative interval, this really only comes into play if you set teh freq_interval to 32 (meaning monthly intervals). You can choose to have something run with one of the following options. If you choose a req_type other than 32, this option is ignored.

Value Description (unit)
1 First
2 Second
4 Third
8 Fourth
16 Last

Next, set the frequency recurrence factor, the number of weeks or months between the scheduled execution of a job.

The last four options are much easier to understand than those in the middle. @active_start_date is the date you want to start this job being active in yyyymmdd format. @active_end_date of course is your end date (99991231 is the same as never). @active_start_time is the start time in hhmmss format, leaving @active_end_time as your end time.

You also have access to sp_update_schedule and sp_delete_schedule in case you need to modify your schedule.

This is a difficult way to build your jobs, until you have to start rolling them out on multiple servers (development, testing, and production). Then you’ll learn to love this method, since you can script your changes, and store them in version control.

If you have any questions on jobs, let me know, I’m here to help!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme