SQL Server Agent Jobs
SQL Server Agent runs scheduled jobs — backups, index maintenance, data imports — and tells you when something fails.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Related tutorials
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)#
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';
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#
- sp_add_job creates the job.
- sp_add_jobstep adds the T-SQL step with a timestamped file name.
- The schedule runs daily (freq_type 4) every 15 minutes (subday type 4 = minutes).
- sp_add_jobserver targets the local server.
- 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#
After a reboot no jobs run. Set the service to Automatic.
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
EXEC msdb.dbo.sp_start_job @job_name = N'ShopDB - Log backup';
Mini quiz#
Which edition does not include SQL Server Agent?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.