Solved

How to disable/enable multiple sql jobs in efficient way

Posted on 2016-10-12
11
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 28

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 28

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 83 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
Independent Software Vendors: 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 83 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 28

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 13

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 251 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 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 83 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 13

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 13

Accepted Solution

by:
Nakul Vachhrajani earned 251 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Do not display comma when no last name 8 48
Windows Server 2003 STD SP2 as a member server of 2016 domain 11 97
Report 8 27
Section based report in SSRS 14 35
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

752 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