SQL Server · Administration

SQL Server Agent Jobs

SQL Server Agent runs scheduled jobs — backups, index maintenance, data imports — and tells you when something fails.

Advanced 5 min Lesson 42 of 44Examples run on SQL Server 2022 and Azure SQL Database.
Before you start:Backup and Restore
0/44 in SQL Server
On this page · 5 min · Advanced

Learning objectives#

  • Start the SQL Server Agent service
  • Create a job with T-SQL steps
  • Add a schedule
  • Get notified on failure

Explanation#

SQL Server Agent is a Windows service installed with Developer, Standard and Enterprise editions (not Express — use Windows Task Scheduler with sqlcmd there). Set it to start automatically in SQL Server Configuration Manager.

In SSMS: SQL Server Agent → Jobs → New Job. General: a name and owner. Steps: one or more T-SQL (or PowerShell) steps, each with what to do on success or failure. Schedules: when it runs. Notifications: whom to tell.

For email, configure Database Mail and an Operator (SQL Server Agent → Operators), then tick “E-mail … When the job fails”.

Job History (right-click the job → View History) shows each run, its duration and the error message of failed steps.

Step-by-step screens#

Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: The same job in T-SQL (msdb procedures)#

SQLThe same job in T-SQL (msdb procedures) Illustrative
USE msdb;
EXEC sp_add_job         @job_name = N'ShopDB - Log backup', @owner_login_name = N'sa';
EXEC sp_add_jobstep     @job_name = N'ShopDB - Log backup', @step_name = N'Backup log',
     @subsystem = N'TSQL', @database_name = N'master',
     @command = N'DECLARE @f NVARCHAR(260) = N''D:\Backup\ShopDB_log_'' + FORMAT(SYSDATETIME(), ''yyyyMMdd_HHmm'') + N''.trn'';
                  BACKUP LOG ShopDB TO DISK = @f WITH COMPRESSION, CHECKSUM;';
EXEC sp_add_schedule    @schedule_name = N'Every 15 minutes', @freq_type = 4, @freq_interval = 1,
     @freq_subday_type = 4, @freq_subday_interval = 15;
EXEC sp_attach_schedule @job_name = N'ShopDB - Log backup', @schedule_name = N'Every 15 minutes';
EXEC sp_add_jobserver   @job_name = N'ShopDB - Log backup';
EXEC sp_update_job      @job_name = N'ShopDB - Log backup', @notify_level_email = 2, @notify_email_operator_name = N'DBA Team';
Expected output
The job appears under SQL Server Agent → Jobs and runs every 15 minutes; a failure emails the DBA Team operator.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. sp_add_job creates the job.
  2. sp_add_jobstep adds the T-SQL step with a timestamped file name.
  3. The schedule runs daily (freq_type 4) every 15 minutes (subday type 4 = minutes).
  4. sp_add_jobserver targets the local server.
  5. notify_level_email = 2 emails on failure.

Real-world use case#

A small company runs full, differential and log backups plus weekly index maintenance as four Agent jobs, and the IT lead gets an email only when one fails.

Common mistakes#

Agent service set to Manual

After a reboot no jobs run. Set the service to Automatic.

No failure notification

Jobs fail silently for weeks. Always notify an operator on failure.

Best practices#

  • Agent service on Automatic
  • Clear job and step names
  • Notify on failure
  • Review job history weekly

Remember this#

Agent = scheduler for SQL Server.

Job = steps + schedule + notifications.

Not in Express edition.

Practice exercise#

Your task: Start a job named “ShopDB - Log backup” immediately.

Show solution
SQL Illustrative
EXEC msdb.dbo.sp_start_job @job_name = N'ShopDB - Log backup';

Mini quiz#

SQL Server Agent Jobs quizMultiple choice Question 1 of 1

Which edition does not include SQL Server Agent?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.