SQL Server JOB
SQL Server JOB
There are a few components of the SQL Server Agent service that you must be aware of before
proceeding forward.
1. Jobs – This is a program that defines the rules about the repetitive execution of one or more
scripts or other utilities within the SQL Server environment
2. Steps – These can be considered as the building blocks of the jobs. Your job can contain one
or multiple steps. Each step executes a specific set of instructions. The next step can be
executed based on the success or failure of a previous step
3. Schedules – These are periodic rules set on the job to execute automatically based on a pre-
defined time slot. These jobs can be scheduled hourly, daily, weekly, monthly, or even on
specific days of the week
4. Alerts – SQL Server generates events that are stored in the Microsoft Windows Application
Log. Whenever the agent finds a match, it fires an alert which is a response to the event
5. Notifications – You can set up email notifications to update about the result of the job
execution. This is mostly done in case of job failures so that the person responsible for the
jobs can take appropriate actions
Head over to Run and type the command services.msc. Click OK once done.
Once you hit OK, the Services window will show up. You can scroll below to find the service with the
name “SQL Server Agent (<<INSTANCE NAME>>)”. As you can see in the figure below, the status
of the service is not running. Let us go and start the service.
Right-click on the service and select Start from the context menu.
It might take a while to start the service. Once the service has started, the status will change
to Running.
Now, you can verify the status of the SQL Server Agent service using the SQL Server Management
Studio as well. Head over to SSMS and navigate to the SQL Server Agent on the bottom of
the Object Explorer.
In this table, we are going to use the following script to insert records.
Creating the first SQL Server Agent job
As we have prepared our database, let us now create the job using the GUI. Right-click on Jobs and
select New Job from the context menu.
As you click on New Job, the window appears on which you can define the properties of the job that
you want to create.
If you look at the figure above, under the General tab, I have provided a valid Name to the job.
The Owner of the job should be a defined user in the SQL Server environment. I am logged on using
my Windows Authentication mode. There is a Description field, where you can provide details
regarding the tasks performed by the job. This is purely for documentation purposes. Finally, make
sure that the Enabled is checked, otherwise, the job will not trigger. Click OK once done and click
on Steps.
Navigate to the Steps section and click on New. Here we will define the steps in the job. For this
article, we will consider a single step, but there can be multiple steps as well.
Provide a name to the step and select the step type. Since we are going to execute a T-SQL
statement, please select Transact-SQL script (T-SQL) from the dropdown. Select the Database on
which the script is to be executed and provide the SQL command as mentioned. Click OK once
done.
Finally, click OK on the Job Properties page. Your job has been successfully scheduled now.
Since the job is scheduled to execute every 10 seconds, we can view the data from the table easily
and verify the execution of the job. A new record will be inserted every 10 seconds from now on.
For more information on these dialog boxes, see Job Categories - Manage Job
Categories and Job Categories Properties - New Job Category.
Task
Create a SQL Server Job Using T-SQL
insert a record to a table with an identity column, and I want to pass the newly
inserted identity value, i.e. scope_identity(), to the next job step.There are many
different types of SQL Server Agent Job steps, such as ActiveX Script, PowerShell
script, SSIS packages, T-SQL script, etc., out of these job steps, the most difficult
one is probably the T-SQL Script type as T-SQL usually does not have an easy way
to write data outside of the scope for the next step to consume, while ActiveX
Script, PowerShell and SSIS package can easily generate an external file or even
So what solutions do we have to address this data exchange issue for T-SQL type
job steps?