-- SQL Server Agent jobs are typically created
-- through SQL Server Management Studio or T-SQL
EXEC msdb.dbo.sp_add_job @job_name = 'NightlyBackup';
Topics
43
Aggregate Functions & GROUP BY
Always On Availability Groups
Backup & Recovery
Change Data Capture (CDC)
Columnstore Indexes
Common Table Expressions (CTEs)
Constraints (Primary Key, Foreign Key, Check & Unique)
Cursors
Data Types & Schema Design
Deadlocks
Dynamic Data Masking
Dynamic SQL
Error Handling with TRY CATCH
Full Text Search
Hash Indexes & Hash Join Operations
Indexes
In-Memory OLTP (Memory-Optimized Tables)
Isolation & Locking
Joins
JSON Support in SQL Server
Linked Servers
Merge Statement (Upsert)
Normalization
Partitioning
Pivoting & Unpivoting Data
Query Optimization & Plans
Query Store
Replication
SQL Server Agent & Job Scheduling
SQL Server Architecture & Editions
SQL Server Profiler & Extended Events
SQL Server Security & Permissions
Stored Procedures & Functions
Subqueries
Temporary Tables & Table Variables
Transactions & ACID
Transparent Data Encryption
Triggers
T-SQL Fundamentals & Syntax
User Defined Functions
Views
Window Functions
XML Data Type & Querying
SQL Server Agent & Job Scheduling
7 questions found
SQL Server Agent is a background service that runs and manages scheduled jobs, such as regular database backups, index maintenance, data synchronization tasks, and custom scripts, letting administrators automate routine work so it runs reliably on a defined schedule without requiring someone to manually trigger it every time.
Real-world example
A company automates its nightly database backup process using a SQL Server Agent job, ensuring backups run consistently every night at the same time without anyone needing to remember to start them manually.
Backup & Recovery;Stored Procedures & Functions
How do you create a basic SQL Server Agent job that runs a T-SQL script on a defined schedule?
BeginnerYou create a new job, add one or more job steps defining the actual T-SQL command or script to run, and attach a schedule specifying how often and when the job should execute, such as daily at a specific time, letting SQL Server Agent handle triggering that job automatically going forward.
EXEC msdb.dbo.sp_add_job @job_name = 'CleanupOldLogs';
EXEC msdb.dbo.sp_add_jobstep @job_name = 'CleanupOldLogs', @step_name = 'DeleteOldRows',
@command = 'DELETE FROM Logs WHERE LogDate < DATEADD(month, -6, GETDATE())';
EXEC msdb.dbo.sp_add_schedule @schedule_name = 'DailyAt2AM', @freq_type = 4, @active_start_time = 020000;
Real-world example
An application automatically cleans up log entries older than six months every night at 2am using a scheduled SQL Server Agent job, keeping the logs table from growing indefinitely without any manual intervention.
Stored Procedures & Functions;Query Optimization & Plans
You configure Database Mail with a valid mail profile, create an operator representing the person or team who should be notified, and then set up a notification on the job itself specifying that an email should be sent to that operator whenever the job fails, giving your team immediate visibility into problems without needing to manually check job history.
EXEC msdb.dbo.sp_add_operator @name = 'DBA Team', @email_address = 'dba@example.com';
EXEC msdb.dbo.sp_update_job @job_name = 'NightlyBackup', @notify_level_email = 2, @notify_email_operator_name = 'DBA Team';
Real-world example
A team receives an immediate email alert whenever their nightly backup job fails, allowing them to quickly investigate and resolve the issue before it puts several days of data at risk.
Backup & Recovery;Error Handling with TRY CATCH
How do you configure a job step to automatically retry if it fails, rather than immediately marking the entire job as failed?
IntermediateYou set the retry attempts and retry interval properties on a specific job step, telling SQL Server Agent to automatically attempt that step again a defined number of times, waiting a specified number of minutes between attempts, before finally marking it as failed if all retry attempts are unsuccessful.
EXEC msdb.dbo.sp_update_jobstep
@job_name = 'DataSync',
@step_id = 1,
@retry_attempts = 3,
@retry_interval = 5;
Real-world example
A data synchronization job automatically retries up to three times with a five minute delay between attempts if it initially fails due to a temporary network issue, often succeeding on a retry without requiring any manual intervention.
Error Handling with TRY CATCH;Linked Servers
How would you design a complex SQL Server Agent job with multiple steps that need to run conditionally based on the success or failure of previous steps?
AdvancedYou configure each job step's on success and on failure actions individually, choosing whether to move to the next step, quit the job reporting success, or quit the job reporting failure, letting you build sophisticated workflows where certain steps only run under specific conditions, such as only sending a success notification if all previous data processing steps completed correctly.
EXEC msdb.dbo.sp_update_jobstep
@job_name = 'ETLProcess',
@step_id = 2,
@on_success_action = 3, -- go to next step
@on_fail_action = 2; -- quit the job reporting failure
Real-world example
A multi step data processing job is designed so that if the data validation step fails, the entire job immediately stops and reports failure, preventing potentially bad data from being loaded into the next stage of the pipeline.
Error Handling with TRY CATCH;Stored Procedures & Functions
How do you monitor and troubleshoot SQL Server Agent job performance and failures across an entire server with many scheduled jobs?
AdvancedYou can query the msdb system database's job history tables directly, such as sysjobhistory, to build custom reports showing job success rates, average duration, and failure trends over time, which is especially useful on a server with many jobs where manually checking each one individually through the graphical interface would be impractical.
SELECT j.name, h.run_date, h.run_status, h.run_duration
FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobhistory h ON j.job_id = h.job_id
WHERE h.step_id = 0
ORDER BY h.run_date DESC;
Real-world example
A database administrator managing dozens of scheduled jobs across several servers builds a custom monitoring report querying sysjobhistory directly, quickly spotting jobs with declining success rates that need investigation.
SQL Server Profiler & Extended Events;Query Optimization & Plans
What security considerations should be kept in mind when configuring the account that SQL Server Agent jobs run under?
IntermediateJobs should generally run using the minimum permissions necessary to complete their specific task, often through a dedicated proxy account rather than using an overly privileged service account, since a job with excessive permissions could cause significant damage if its underlying script contains a mistake or is somehow compromised.
EXEC msdb.dbo.sp_add_proxy @proxy_name = 'LimitedBackupProxy', @credential_name = 'BackupCredential';
EXEC msdb.dbo.sp_grant_proxy_to_subsystem @proxy_name = 'LimitedBackupProxy', @subsystem = 'TSQL';
Real-world example
A company sets up a dedicated proxy account with only the specific permissions needed to run backup jobs, rather than running every scheduled job under a powerful administrative account that could cause serious harm if a script contained a mistake.
SQL Server Security & Permissions;Backup & Recovery