FGF-College-Work / Forum

:beer: Espaço dedicado a discussões e tira dúvida sobre disciplinas e conteúdo tecnológico.
MIT License
13 stars 4 forks source link

Scripting all Agent Jobs Using SQL Server Management Studio #158

Open marcialwushu opened 5 years ago

marcialwushu commented 5 years ago

I recently came across a way to script up all agent jobs in one go within SQL Server Management Studio (SSMS). Previously I used to right click on each job and select ‘Script Job as’ to script up each one individually, but this can be very labourious if there are a lot of jobs to do.

To script all jobs, just open the ‘Object Explorer Details’ from the View menu in SSMS, or press the F7 key. Click on any job in the Object Explorer window and a list of all the agent jobs appears in the ‘Object Explorer Details’ window.

Select all the jobs you want to script (press the Ctrl button while clicking to select individual jobs) and then right click and select the scripting option you want. This will then create all the selected jobs as a single query. You can script to a query window, file etc.


ORIGINAL

marcialwushu commented 5 years ago

Create a Transact-SQL Job Step

This topic describes how to create a Microsoft SQL Server Agent job step that executes Transact-SQL scripts in SQL Server 2017 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.

In This Topic

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

Using Transact-SQL

To create a Transact-SQL job step

-- 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  

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.


DOCS MICROSFT