SQL Server Agent is a component used for the database tasks automation. For instance, 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 service is disabled. First, we enable it and start it manually. Then, we configure the SQL Server job, using SQL Server Management Studio and the system stored procedures of the MSDB database.

This article explains how to create a SQL Server Job using the system stored procedures of the MSDB database.

The system stored procedures of the MSDB database

SQL Server uses the following ones:

  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:
  • @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.

ValueDescription0The outcome of the job will not be written to the event log.1If the job executes successfully, the outcome will be written to the event viewer2 (default value)If the job fails, the outcome and error message will be written to the event viewer3The outcome of the job is written to the event viewer.

  • @notify_level_email: This parameter serves to send the email about the SQL 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 @notify_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 1. Note that 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.

2. Sp_add_jobserver: This stored procedure serves to specify the target server for the SQL Job execution. The procedure accepts the following arguments:

  • @job_id: It is a UNIQUEIDENTIFIER of the SQL Job. The default value of this argument is NULL.
  • @job_name: It is the name of the SQL 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 target server hostname.

3. sp_add_jobstep: This stored procedure works for adding the job step in SQL Job. The procedure uses the following arguments:

  • @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 SYSNAME with NULL as the 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_sucess_action: This value indicates the action that should be performed after the job step completes successfully. The values can be any of the following:

#programming #sql server #automation #database administration #sql server #t-sql queries

Configure SQL Jobs in SQL Server using T-SQL
2.40 GEEK