Configure SQL Jobs in SQL Server using T-SQL

Configure SQL Jobs in SQL Server using T-SQL

Using SQL Server Management Studio. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand SQL Server Agent, create a new job or right-click an existing job, and then click Properties. In the Job Properties dialog, click the Steps page, and then click New.

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

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

List of Available Database for Current User In SQL Server

List of Available Database for Current User In SQL Server. Do you know yet? Let's explore it with us now.

SCHEMAS in SQL Server -MS SQL Server – Zero to Hero Query Master

This is part 3 of “MS SQL Server- Zero to Hero” and in this article, we will be discussing about the SCHEMAS in SQL SERVER. Before getting into this article, please consider to visit previous articles in this series from below.

System Databases in SQL Server

In SSMS, we many of may noticed System Databases under the Database Folder. But how many of us knows its purpose?. In this article lets discuss about the System Databases in SQL Server.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.