Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let’s discuss it one by one.
SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production servers during the non-Business hours only. So, we create a SQL Server job of running index maintenance and schedule it for “off” hours. When we install SQL Server, the SQL Server Agent is disabled. So at first we will enable it and start it manually. Then, we will configure the SQL Server job using SQL Server Management Studio and the system stored procedures of the MSDB database. So, this article explains how to create SQL Server Job using the system stored procedures of the MSDB database.
System stored procedures of the MSDB database :
SQL Server uses the following ones are as follows.
Query-1 : sp_add_job :
The procedure is for creating a new job. If it is successful, it returns @job_id. The following arguments are applicable as follows.
- @job_name –
It is a unique job name.
- @enabled –
Job is enabled or disabled. Once a job is created, you can set the parameter’s value as 1 to enable the job.
- @notify_level_eventlog –
This parameter is used for writing the status of the SQL Job in Windows event viewer.
|0||The outcome of the job will not be written to the event log.|
|1||If the job executes successfully, the outcome will be written to the event viewer|
|2(default value)||If the job fails, the outcome and error message will be written to the event viewer.|
|3||The outcome of the job is written to the event viewer.|
- @notify_level_email –
It will send the mail on the basis of job outcome. The valid values of the parameter are the same as the @notify_level_eventlog argument values.
- @notify_level_page –
This parameter serves to send the pager notification of the SQL Job outcome. The valid values of the parameters are the same as the @notiify_level_eventlog argument values.
- @delete_level –
This parameter serves to delete the job after completion. In this case, the value of the parameter should be
- Note –
The default value is 0; then, it won’t delete the job after completion.
- @category_level –
This parameter indicates the job category values. The default value is Null.
- @owner_login_name –
The value is the domain name or the job owner’s SQL Login name.
Query-2: Sp_add_jobserver :
For SQL job execution, it specifies the target server. It accepts the following arguments as follows.
- @job_id –
In SQL job, it identifies the unique values, and it is a UNIQUEIDENTIFIER. Its default value is NULL.
- @job_name –
In SQL job, this parameter specifies the name of job.
- @server_name –
It is the name of the server where you want to run the SQL Job. The default argument value can be the local server (LOCAL) or the targeted server hostname.
Query-3: sp_add_jobstep :
This stored procedure works for adding the job step in SQL Job. The procedures use the following arguments as follows.
- @job_name –
The name of the job in which you are adding the step. It is an SYSNAME with NULL as the default value.
- @step_name –
The name of the step. It is an SYSYNAME with NULL as default value.
- @step_id –
The sequential ID of the Job step. It is an incremental number without a gap. It is an INT value, and the default value is NULL.
- @cmdexec_success_code –
This value is returned by the CmdExec subsystem. It indicates whether the command execution was successful. The code is int value with 0 as a default value.
- @on_success_action –
This value indicates the action that should be performed after the job step completes successfully. The values can be any of the following :
|1||Quit the job and return success.|
|2||Quit the job and return failed.|
|3||Go to the next job step|
|4||Go to the step id of on_success_step_id|
- @on_fail_action –
Specify what action to be performed if the job steps fail. It is an INT value, and the default value is NULL.
- @retry_attempt –
Specify the number of retry attempts after the job step failure. It is an INT value, and the default value is NULL.
- @retry_interval –
Set the time interval between two SQL job step failure attempts. It is an INT value, and the default value is NULL.
- @Subsystem –
Specify the name of the subsystem used by SQL Server Agent to execute the command. The valid values are the following:
|CmdExec file(*.exe,*.bat)||Operating system command or the executable|
|ANALYSYISQUERY||SQL Server analysis service queries, for example, MDX, DMX|
|ANALYSISQUERY||SQL Server analysis service command, for example, XMLA.|
|SSIS||SQL Server integration service package.|
|PowerShell||PowerShell command on the script.|
|T-SQL||T-SQL Query or Stored procedure|
|Distribution||SQL Server replication distributor agent.|
|Snapshot||SQL Server replication snapshot agent.|
|LogReader||SQL Server replication Log reader agent.|
|QueueReader||SQL Server replication queue reader.|
- @command –
Specify the command that the SQL Server Agent Service should execute through the subsystem. The data type is varchar(max), and the default value is NULL.
- @Database_name –
Specify the name of the database where you want to run the command. This parameter is useful when you are running a T-SQL script using SQL Server Agent.
The stored procedures serve to create the SQL Job schedule. This procedure uses the following arguments as follows.
- @job_name –
Specify the name of the SQL Job. The schedule will be made for the SQL Job specified in the @job_name argument.
- @name –
Name of the schedule. The data type is varchar, and the default value is NULL.
- @enabled –
Set 1 to enable the schedule or 0 to disable the schedule.
- @freq_types –
Indicates the time of the SQL Job execution. The data type of the parameter is INT, and the default value is 0. The valid values are any of the following :
|1||The JOB will be executed only One Time.|
|64||Executes the job when the SQL Server Agent Services start.|
|128||Execute the SQL Job when the server is idle.|
- @freq_interval –
Indicates the day of the SQL job execution. The data type is INT, and the default value is 0. The value depends on the value specified in the @freq_type parameter. The valid values are any of the following :
|Value||Effect on the job schedule|
|1(Once)||The @Freq_interval will not be used.|
|4(Daily)||Every @freq_interval days|
The value of the @Freq_interval can be any of the following as follows.
|16||Run the job on the @Freq_interval day of the month|
|64||The @Freq_interval will not be used.|
|128||The @Freq_interval will not be used.|
- @freq_subday_type –
Specify the unit of the freq_subday_interval. The data type is INT, and the default value is NULL.
- @active_start_date –
Set the date when you want to start the job execution. The data type is INT, and it does not have a default value. The date format is YYMMDD. The value must be greater or equal than 19900101.
- @active_end_date –
Specify the date when to stop the job execution. The data type is INT, with no default value. The date format is YYMMDD, and the value must be greater or equal than 19900101.
- @active_start_time –
Specify the time when you want to start the job execution. The data type is INT, without a default value . The time format is HHMMSS.
- @active_end_time –
Specify the time when you want to stop the job execution. The data type is INT, without a default value. The time format is HHMMSS.