Solved

How to disable/enable multiple sql jobs in efficient way

Posted on 2016-10-12
11
90 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 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

by:Pawan Kumar Khowal
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 18

Expert Comment

by:Pawan Kumar Khowal
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 11

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 18

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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 11

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 11

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

744 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