Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Managed Instance
Important
On Azure SQL Managed Instance, most SQL Server Agent features are supported. For more information, see Azure SQL Managed Instance T-SQL differences.
This article explains how to create a SQL Server Agent job using SQL Server Management Studio (SSMS), Transact-SQL (T-SQL), or SQL Server Management Objects (SMO).
To add job steps, schedules, alerts, and notifications that can be sent to operators, see the links to articles in the Related content section.
Prerequisites
- User must be a member of SQL Server Agent fixed database roles or the sysadmin role.
- Only job owners or members of the sysadmin role can modify jobs.
- Assigning a job to another login doesn't guarantee sufficient permissions to run the job.
Security considerations
- Only the sysadmin role can change the job owner.
- The sysadmin role can assign job ownership to other users and run any job.
- Jobs with steps requiring proxy accounts need to ensure the new owner has access to those proxies, or the job fails.
For detailed security information, see Implement SQL Server Agent security
Use SQL Server Management Studio
In Object Explorer, expand the server where you're creating the job.
Expand SQL Server Agent.
Right-click Jobs and select New Job....
On the General page, configure job properties. For more information, see General page.
On the Steps page, configure the job steps. For more information, see Steps page.
On the Schedules page, set job schedules. For more information, see Schedules page.
On the Alerts page, configure job alerts. For more information, see Alerts page.
On the Notifications page, configure job completion notifications. For more information, see Notifications page.
On the Targets page, configure the target servers. For more information, see Targets page.
Select OK to save the job.
Use Transact-SQL
In Object Explorer, connect to the server.
Open a New Query window.
Copy and paste the following script:
USE msdb; GO EXECUTE dbo.sp_add_job @job_name = N'Weekly Sales Data Backup'; GO EXECUTE sp_add_jobstep @job_name = N'Weekly Sales Data Backup', @step_name = N'Set database to read only', @subsystem = N'TSQL', @command = N'ALTER DATABASE SALES SET READ_ONLY', @retry_attempts = 5, @retry_interval = 5; GO EXECUTE dbo.sp_add_schedule @schedule_name = N'RunOnce', @freq_type = 1, @active_start_time = 233000; GO EXECUTE sp_attach_schedule @job_name = N'Weekly Sales Data Backup', @schedule_name = N'RunOnce'; GO EXECUTE dbo.sp_add_jobserver @job_name = N'Weekly Sales Data Backup'; GO
For more information, see:
Use SQL Server Management Objects
To create a SQL Server Agent job using SQL Server Management Objects (SMO):
Call the Create method of the Job class by using a programming language that you choose, such as Visual Basic, Visual C#, or PowerShell. For example code, see Scheduling Automatic Administrative Tasks in SQL Server Agent.