Solved

How to disable/enable multiple sql jobs in efficient way

Posted on 2016-10-12
11
117 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 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 42

Assisted Solution

by:EugeneZ
EugeneZ 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

829 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