0% found this document useful (0 votes)
30 views

SQL Server JOB

Here are a few ways to pass data between T-SQL job steps in SQL Server Agent: 1. Use a table to store the output from the first step and read it in the second step. Insert the identity value into a table in the first step, then select it in the second step. 2. Use SQL Server variables. Assign the identity value to a SQL Server variable in the first step, then reference the variable in the second step. 3. Use the OUTPUT clause. Insert the row in the first step and capture the identity value using OUTPUT INTO, then select from the table in the second step. 4. Use a temporary table. Insert the identity value into a temporary table in the first step

Uploaded by

abdul.rafae221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

SQL Server JOB

Here are a few ways to pass data between T-SQL job steps in SQL Server Agent: 1. Use a table to store the output from the first step and read it in the second step. Insert the identity value into a table in the first step, then select it in the second step. 2. Use SQL Server variables. Assign the identity value to a SQL Server variable in the first step, then reference the variable in the second step. 3. Use the OUTPUT clause. Insert the row in the first step and capture the identity value using OUTPUT INTO, then select from the table in the second step. 4. Use a temporary table. Insert the identity value into a temporary table in the first step

Uploaded by

abdul.rafae221
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab 5 Manual

SQL Server Agent job


This topic describes how to create a SQL Server Agent job in SQL Server by using SQL Server
Management Studio, Transact-SQL, or SQL Server Management Objects (SMO).

SQL Server Agent


All these tasks are repetitive and can be automated using the SQL Server Agent. This gives us the
flexibility that the job will be executed as per the rules defined in the schedule and there is minimal
human intervention required in order to execute these jobs. Once the jobs are executed, you can
view the history if the execution was successful or failed. In case of a failure, there is an option to
restart the job manually.

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

Find the windows service


Now that we have some idea about the SQL Server Agent, let us now go ahead and start the service
if not already done. In my machine, the service is currently not running, and I am going to start it
from scratch. You can follow the steps below to start the agent service on your machine.

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.

Preparing the database


Now that our agent service is up and running, let us create our first job using the graphical user
interface. We can also create jobs using T-SQL scripts; however, it is out of scope for this article and
will be covered in some later articles. To demonstrate the execution of the job, we will create a small
table in a SQL Server database that will store the date and time of the execution along with a
random number. This is a dummy table just to demonstrate the feature. You can use the following
script to create the table.

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.

Click on Schedules and select New.


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.

Click on Schedules and select New.


On the New Job Schedule window, provide a name for the schedule. Most often, we keep a
descriptive name so that it is easier to understand the nature of the schedule. Select the Schedule
Type as Recurring since we are going to execute the job repeatedly. Select
the Frequency as Daily and Daily Frequency as every 10 seconds. In real applications, it is usually
scheduled every hour or every day depending on the requirements. Click OK once done.

Finally, click OK on the Job Properties page. Your job has been successfully scheduled now.

Viewing the data


Let us now execute the following command to view the data.

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.

Viewing Execution History


Alternatively, you can also view the execution history of the job by right-click and selecting View
History from the context menu.
This will open the Log File Viewer window, where you can see the status of all the executions so far.
All successful executions are highlighted in green while failed executions will be highlighted in red.
2. Manage Job Categories
1. In Object Explorer, click the plus sign to expand the server where you want to
create a job category.
2. Click the plus sign to expand SQL Server Agent.
3. Right-click the Jobs folder and select Manage Job Categories.
4. In the Manage Job Categories server_name dialog box, click Add.
5. In the new dialog box, in the Name box, enter a name for the new job category.
6. Select the Show all jobs check box. Select one or more jobs for the new category
by checking the boxes corresponding to the jobs.
7. Click OK.
8. In the Manage Job Categorie sserver_name dialog box, click Refresh to ensure
that the new job category is active. If everything looks as expected, close this
dialog box.

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

access to the OS environment variables for data exchange purposes.

So what solutions do we have to address this data exchange issue for T-SQL type

job steps?

You might also like