How to Create SQL Server Agent Job: SSMS

SQL Server Agent Job is one of the SQL Scheduler which Can Execute Procedures , Triggers, views, and any select, Insert Query, or task at a scheduled time
In another way, we can say that a SQL agent job is a task of automation for executing various database maintenance, reporting, and administrative tasks.

SQL SERVER AGNET JOB allows us to schedule and manage the jobs; these jobs can be run at specified intervals, timings, or events.

In this article, we will learn the process of creating SQL Server Agent jobs using SQL Server Management Studio (SSMS).

In a SQL Agent Job, we can schedule a daily backup and restore, or we can do any insert, update, or select using a SQL script.

Suppose we have to insert data using sql script daily, Monthly yearly, or at any specific time, like for every 5 minutes or every 30 minutes We can schedule the Execution using SQL Agent job

Prerequisites

Before you can create a SQL Server Agent job, you should be aware of the following:

SQL Server Installed: SQL Server must be installed and running on your server.

SQL Server Management Studio (SSMS): You should have SSMS installed on your system.

SQL Server Agent Service should be running on your computer or server. You can Start Agent service from Services.msc or you can from start from SQL server Management Studio in SQL Server Agents option

Steps to Create SQL Server Agent Job

Step 1: Connect to SSMS

Open your SQL Server Management Studio and connect to your SQL Server instance using a username and password, or you can login to it by Windows authentication if you have mixed authentication modes.

Step 2: Expand SQL Server Agent

Expand SQL Server Agent and then right-click on ‘Jobs’ and Select ‘New Job’

Create sql server agent job step1

A new window will appear, allowing you to configure the job. Here are some settings you need to configure:

You will see six different (optional) pages in the upper right corner like General, Steps,Scheduler,Alert,Notification and Target

SQL Server Agent Job

Step 3: General Option

In General Options, you have to fill in the details of your sql agent job, like name, owner, and description.

Name : Provide a name for your job.


Owner: Choose the owner of the job. Typically, this is a user with sufficient permissions to execute the tasks or script in the job.
Generally, you can choose ‘a’sa’ user who has a sysadmin role on the SQL server with full permissions.


Description: Provide a description of the job’s purpose. Enter information about the job and what that job will do.

create sql server agent job step2
Step 4: Add Job Steps

This is most important factor in Sql agent job
In Sql server Agent, there are many steps; each step has different work in SQL Server Agent Job
Click on the “Steps” page on the left to add a new job step and Click on ‘New’ from left bottom of the step page

step 3 in sql agent

In Steps Jobs option, you have to fill in the Details of Your Steps like name, type, run as, database, and Command

steps in sql agent

Step Name: Give your step name with a description.


Type: Select the type of job step like Power shell, Operating system, Transact-SQL Script (T-SQL), Server Integration Services (SSIS) package, SQL Server Analysis Services command, etc.

Download SSMS


Run As: Specify the credentials under which the job step should run.


Database: Choose the database for the job step (if applicable) you can use Database name in your SCP instead of this Database factor


Command: Enter the command or script for the job step Here you can enter your SQL script , command or any operating system batch file, etc


command is the most important factor in Job, which will maintain or manipulate the database.
If you have to backup database daily by using sql agent, then you can put Backup Sql script in Command option

https://itdevelopement.com/category/sql/

Step 5: Define Schedule

It is uesd to schedule when the job should run, click on the “Schedules” page on the left and add a ‘New’ schedule.

step 5

In Schedule Options you have to fill the Details of Schedule for which timing the job will run like Name,Frequency, Start Date/Time,Recurring, Duration and Summary

step 6

Name: Give the name to your schedule; you can name it like a job name or a step name.


Frequency: Define execution frequency in days, weeks, months, and years (e.g., daily, weekly, monthly).
you will see a daily Frequency option depending on what frequency you choose, like occues once @ [Timing] or occurs Every [hours, Minutes, Seconds]


Start Date/Time: Specify when the job should start running and define Start time of Job execution


Duration: Specify the duration of job, from which date and time will start and for which date it will stop, i.e. Start date and end date of job


Recurring: Configure recurrence options based on the chosen frequency.

Step 6: Alerts

Alerts in sql server agent is a notification for event occured for the scheduled task such as Job Start , Job failure, Job Success , system resouce that meet specific threshold
SQL Server Agent Support Three Diffeent Alerts

1.SQL Server Performance Event Alert
2.SQL Server Event Alert
3.WMI Event Alert

Step 7: Notification & Target (Optional)

If you want to receive notifications on specific event like when the Job Start, job success (or fails), then you can configure its setting on the “Notifications” page.

Step 8: Save and Execute

Once you’ve completed all necessary configurations and all the settings, click “OK” to save the job.


To execute this job manually, right-click on the job in the Object Explorer and select the “Start Job at Step” option to run the job manually.
Or it will RUN automatically in it’s Scheduled Timing

SQL Server Agent jobs are powerful tools for automating routine tasks and ensuring that critical database operations are performed consistently and on schedule. By following the steps outlined in this article, you can create and manage

SQL Server Agent jobs are executed at the scheduled time for your database administration and maintenance processes.

SQL Script for SQL agent Job

USE [msdb]
GO

/****** Object:  Job [SQl Server Agent Job Test]    Script Date: 08-10-2023 10:10:19 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 08-10-2023 10:10:19 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'SQl Server Agent Job Test', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'this is test job created for how to create sql server agent job in ssms tutorial', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [test job for tutorial]    Script Date: 08-10-2023 10:10:20 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'test job for tutorial', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'Insert into Employee (EmployeeID,EmployeeName,EmployeeEmail,EmployeeDOB)

 select ID, Name,Email,DOB from EmpMaster', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'test job for ssms tutorial', 
		@enabled=1, 
		@freq_type=4, 
		@freq_interval=1, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=0, 
		@freq_recurrence_factor=0, 
		@active_start_date=20231008, 
		@active_end_date=20231008, 
		@active_start_time=120000, 
		@active_end_time=235959, 
		@schedule_uid=N'0122849d-9370-4688-9ce6-550735ada4e3'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

Click Here for SQ Tutorial

all options in sql agent job

FAQ SQL Server Agent Job

1.How to see Agent Job Execution History ?

To see Execution History of your Job Right-Clcik on your ‘Job’ (Your Job name) and Click on ‘View History’
If your job is failed to execute then you can see the detailed error description in ‘View History’ by expanding the failed job

2.How To Edit SQL Agent Job ?

To Edit your Existing SQL Agent Job Right-Clcik on your ‘Job’ (Your Job name) and Click on ‘Properties’, you will see all the Pages to edit like General, Scheduler, Steps Notifications, etc.

3.How to Terminate SQL Agent Job?

To Stop your Current Executing Job Right-Clcik on your ‘Job’ (Your Job name) and Click on Stop Job

4.How TO Enable or Disable SQL Agent Job?

To disable and enable your job, right-click on your ‘Job’ (your job name) and click on the Disable/Enable option.

5.How to RUn SQL Server Agent Job Manually ?

To Run SQL Server Agent Job Manually in SQL Server Management Studio, Right-Click on ‘Job’ (Your Job name), which you want to execute, and then Click on ‘Start Job At Step’

run sql server agent job manually

6.How to do I start and Stop SQL Server Agent Service?

To start and STop SQL Server Agent Service you can Start or Stop in two ways

  1. SQL Server Management Studio.
    To Start or Stop Service from SSMS, Right-click on SQL Server Agent and click on ‘Stop/Start’
  2. Services.msc
    To Start or Stop Service from services.msc Enter windows serach box ‘services’ open them and sqarch for SQL then choose ‘SQL Server Ageent’ (Your Instance Name) e.g. SQL Server Agent (MSSQLSERVER) right click on it and click on Start or stop, You can Restart the service from both Options.

1 thought on “How to Create SQL Server Agent Job: SSMS”

Leave a Comment