Solved

How to disable/enable multiple sql jobs in efficient way

Posted on 2016-10-12
11
99 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 24

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 24

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

Assisted Solution

by:ScottPletcher
ScottPletcher 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 24

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 12

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 24

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 12

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 12

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now