Create a Transact-SQL Job Step - SQL Server Agent (2024)

  • Article

Applies to: Create a Transact-SQL Job Step - SQL Server Agent (1) SQL Server Create a Transact-SQL Job Step - SQL Server Agent (2) Azure SQL Managed Instance

This topic describes how to create a Microsoft SQL Server Agent job step that executes Transact-SQL scripts in SQL Server by using SQL Server Management Studio, Transact-SQL, or SQL Server Management Objects.

These job step scripts may call stored procedures and extended stored procedures. A single Transact-SQL job step can contain multiple batches and embedded GO commands. For more information on creating a job, see Creating Jobs.

Before You Begin

Security

For detailed information, see Implement SQL Server Agent Security.

Using SQL Server Management Studio

To create a Transact-SQL job step

  1. In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.

  2. Expand SQL Server Agent, create a new job or right-click an existing job, and then click Properties.

  3. In the Job Properties dialog, click the Steps page, and then click New.

  4. In the New Job Step dialog, type a job Step name.

  5. In the Type list, click Transact-SQL Script (TSQL).

  6. In the Command box, type the Transact-SQL command batches, or click Open to select a Transact-SQL file to use as the command.

  7. Click Parse to check your syntax.

  8. The message "Parse succeeded" is displayed when your syntax is correct. If an error is found, correct the syntax before continuing.

  9. Click the Advanced page to set job step options, such as: what action to take if the job step succeeds or fails, how many times SQL Server Agent should try to execute the job step, and the file or table where SQL Server Agent can write the job step output. Only members of the sysadmin fixed server role can write job step output to an operating system file. All SQL Server Agent users can log output to a table.

  10. If you are a member of the sysadmin fixed server role and you want to run this job step as a different SQL login, select the SQL login from the Run as user list.

Using Transact-SQL

To create a Transact-SQL job step

  1. In Object Explorer, connect to an instance of Database Engine.

  2. On the Standard bar, click New Query.

  3. Copy and paste the following example into the query window and click Execute.

    -- creates a job step that uses Transact-SQL USE msdb; GO EXEC sp_add_jobstep @job_name = N'Weekly Sales Data Backup', @step_name = N'Set database to read only', @subsystem = N'TSQL', @command = N'ALTER DATABASE SALES SET READ_ONLY', @retry_attempts = 5, @retry_interval = 5 ; GO 

For more information, see sp_add_jobstep (Transact-SQL).

Using SQL Server Management Objects

To create a Transact-SQL job step

Use the JobStep class by using a programming language that you choose, such as Visual Basic, Visual C#, or PowerShell.

Create a Transact-SQL Job Step - SQL Server Agent (2024)
Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6102

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.