Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to disable/enable multiple sql jobs in efficient way

Posted on 2016-10-12
11
Medium Priority
?
274 Views
Last Modified: 2016-10-17
Guys,

I was thinking to perform some multiple update with sql job, which to set them disabled at specific time and again set it again enable, here is my query, does anyone here have better than a query below to do update?


USE MSDB;
GO
UPDATE J
SET J.Enabled = 0
FROM MSDB.dbo.sysjobs J
INNER JOIN MSDB.dbo.syscategories C
ON J.category_id = C.category_id
WHERE C.[Name] in
('JOB1','JOB2','JOB5','JOB20'
)
GO
0
Comment
Question by:motioneye
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41840462
Try..

USE MSDB
GO

UPDATE m 
SET m.Enabled = 0
FROM MSDB.dbo.sysjobs m
WHERE name IN ( 'a' , 'b' )

GO

Open in new window


The MSDB.dbo.syscategories table will be used if we want to disable Jobs By Job Category.
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41840471
Update - In your query you are using

WHERE C.[Name] in ('JOB1','JOB2','JOB5','JOB20'

this c.Name is not the jobname it is the category name.

Hope it helps.
0
 
LVL 43

Assisted Solution

by:Eugene Z
Eugene Z earned 332 total points
ID: 41840475
just note:
if a job is running -- it will keep running after you disable it (in the middle of run)

Why do not use jobs schedules (you can have several) you can have 1  for several jobs?

https://msdn.microsoft.com/en-us/library/ms191439.aspx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 332 total points
ID: 41840489
You shouldn't update the jobs table directly.  Instead, you should use proc:
EXEC msdb.dbo.sp_update_job
to disable jobs, or make other job-level changes.
0
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41840506
If you need list of jobs and then decide...

SELECT 'Exec MSDB.dbo.sp_update_job @job_name = N''' + a.Name + ''', @Enabled = 0'
  FROM msdb..sysjobs a 
  JOIN msdb..syscategories b
    ON a.category_id = b.category_id
 WHERE a.[Enabled] = 1 --AND a.name IN ( 'a' , 'b' ) 

Open in new window


Sample Output...

Exec MSDB.dbo.sp_update_job @job_name = N'syspolicy_purge_history', @Enabled = 0
0
 
LVL 14

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 1004 total points
ID: 41840803
Please, please do not update the system tables directly! I had written an article on the side effects of doing so on SQLServerCentral.com 2 years ago (http://www.sqlservercentral.com/articles/SQL+Server+Agent/92877/)

For one the agent will not even realize a change has been made and you will need to restart the services, resulting in an outage.

Secondly, updating system tables is not supported. The official documentation from Microsoft in the MSDN/Books On Line (BOL) documentation (http://msdn.microsoft.com/en-us/library/ms179932) opens with the following lines:
The system tables should not be changed directly by any user.

Use the system stored procedures in msdb to make changes to the jobs, job steps and the schedules. Doing so prevents outages and ensures that all necessary subsystems are notified about the change.
0
 
LVL 35

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 332 total points
ID: 41841193
Agree with Nakul, Always use sp to modify the system tables.  Below is the approach one should follow. You can also pass the names if you want. or you can get a list and decide from there.

--
SELECT 'Exec MSDB.dbo.sp_update_job @job_name = N''' + a.Name + ''', @Enabled = 0'
  FROM msdb..sysjobs a 
  JOIN msdb..syscategories b
    ON a.category_id = b.category_id
 WHERE a.[Enabled] = 1 --AND a.name IN ( 'a' , 'b' ) 
--

Open in new window

0
 

Author Comment

by:motioneye
ID: 41842184
Hi

Your suggestion  41841193  to disabled / enable the job via sp_update_job sound promising and much safer. But how about  if I want to run this in the sql jobs instead of me manually firing it up in ssms query ? My situation is to disabled teh job at specific time then again enable the job, this going to happen at each week midnight.
0
 
LVL 14

Expert Comment

by:Nakul Vachhrajani
ID: 41842193
You need to get the job id based on the job name. All the other system stored procedures work on the job Id. So as long as you know the job name, you should be all set to embed this in a custom query/set of queries.
0
 

Author Comment

by:motioneye
ID: 41843178
Hi,
Could someone help me to embded the query below, upon generating the exec syntax  the next step for the script is to run the procedure.

SELECT 'Exec MSDB.dbo.sp_update_job @job_name = N''' + a.Name + ''', @Enabled = 0'
  FROM msdb..sysjobs a
  JOIN msdb..syscategories b
    ON a.category_id = b.category_id
 WHERE a.[Enabled] = 1 --AND a.name IN ( 'a' , 'b'
0
 
LVL 14

Accepted Solution

by:
Nakul Vachhrajani earned 1004 total points
ID: 41843188
Here you go:

DECLARE @manipulateJobs NVARCHAR(MAX) = NULL

SELECT @manipulateJobs = COALESCE(@manipulateJobs, '') + 'Exec MSDB.dbo.sp_update_job @job_name = N''' + a.Name + ''', @Enabled = 0;'
FROM msdb..sysjobs a 
JOIN msdb..syscategories b
ON a.category_id = b.category_id
WHERE a.[Enabled] = 1;

EXEC sp_executesql @sql = @manipulateJobs;

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question