View (and Disable) SQL Agent Jobs with TSQL
Recently, I wanted to find a list of all SQL Agent Jobs running on various servers. I was able to view this in SSMS, of course, but I wanted to be able to copy/paste and toss this into a spreadsheet. So instead of using SSMS, I wrote the script below, with significant help from the sysschedules entry in BOL, to show me the same information, using T-SQL. I also include a script to disable the job, because that's just how I roll.
Declare @weekDay Table ( mask int , maskValue varchar(32) ); Insert Into @weekDay Select 1, 'Sunday' Union All Select 2, 'Monday' Union All Select 4, 'Tuesday' Union All Select 8, 'Wednesday' Union All Select 16, 'Thursday' Union All Select 32, 'Friday' Union All Select 64, 'Saturday'; With myCTE As( Select sched.name As 'scheduleName' , sched.schedule_id , jobsched.job_id , Case When sched.freq_type = 1 Then 'Once' When sched.freq_type = 4 And sched.freq_interval = 1 Then 'Daily' When sched.freq_type = 4 Then 'Every ' + Cast(sched.freq_interval As varchar(5)) + ' days' When sched.freq_type = 8 Then Replace( Replace( Replace(( Select maskValue From @weekDay As x Where sched.freq_interval & x.mask <> 0 Order By mask For XML Raw) , '"/><row maskValue="', ', '), '<row maskValue="', ''), '"/>', '') + Case When sched.freq_recurrence_factor <> 0 And sched.freq_recurrence_factor = 1 Then '; weekly' When sched.freq_recurrence_factor <> 0 Then '; every ' + Cast(sched.freq_recurrence_factor As varchar(10)) + ' weeks' End When sched.freq_type = 16 Then 'On day ' + Cast(sched.freq_interval As varchar(10)) + ' of every ' + Cast(sched.freq_recurrence_factor As varchar(10)) + ' months' When sched.freq_type = 32 Then Case When sched.freq_relative_interval = 1 Then 'First' When sched.freq_relative_interval = 2 Then 'Second' When sched.freq_relative_interval = 4 Then 'Third' When sched.freq_relative_interval = 8 Then 'Fourth' When sched.freq_relative_interval = 16 Then 'Last' End + Case When sched.freq_interval = 1 Then ' Sunday' When sched.freq_interval = 2 Then ' Monday' When sched.freq_interval = 3 Then ' Tuesday' When sched.freq_interval = 4 Then ' Wednesday' When sched.freq_interval = 5 Then ' Thursday' When sched.freq_interval = 6 Then ' Friday' When sched.freq_interval = 7 Then ' Saturday' When sched.freq_interval = 8 Then ' Day' When sched.freq_interval = 9 Then ' Weekday' When sched.freq_interval = 10 Then ' Weekend' End + Case When sched.freq_recurrence_factor <> 0 And sched.freq_recurrence_factor = 1 Then '; monthly' When sched.freq_recurrence_factor <> 0 Then '; every ' + Cast(sched.freq_recurrence_factor As varchar(10)) + ' months' End When sched.freq_type = 64 Then 'StartUp' When sched.freq_type = 128 Then 'Idle' End As 'frequency' , IsNull('Every ' + Cast(sched.freq_subday_interval As varchar(10)) + Case When sched.freq_subday_type = 2 Then ' seconds' When sched.freq_subday_type = 4 Then ' minutes' When sched.freq_subday_type = 8 Then ' hours' End, 'Once') As 'subFrequency' , Replicate('0', 6 - Len(sched.active_start_time)) + Cast(sched.active_start_time As varchar(6)) As 'startTime' , Replicate('0', 6 - Len(sched.active_end_time)) + Cast(sched.active_end_time As varchar(6)) As 'endTime' , Replicate('0', 6 - Len(jobsched.next_run_time)) + Cast(jobsched.next_run_time As varchar(6)) As 'nextRunTime' , Cast(jobsched.next_run_date As char(8)) As 'nextRunDate' From msdb.dbo.sysschedules As sched Join msdb.dbo.sysjobschedules As jobsched On sched.schedule_id = jobsched.schedule_id Where sched.enabled = 1 ) Select job.name As 'jobName' , sched.scheduleName , sched.frequency , sched.subFrequency , SubString(sched.startTime, 1, 2) + ':' + SubString(sched.startTime, 3, 2) + ' - ' + SubString(sched.endTime, 1, 2) + ':' + SubString(sched.endTime, 3, 2) As 'scheduleTime' -- HH:MM , SubString(sched.nextRunDate, 1, 4) + '/' + SubString(sched.nextRunDate, 5, 2) + '/' + SubString(sched.nextRunDate, 7, 2) + ' ' + SubString(sched.nextRunTime, 1, 2) + ':' + SubString(sched.nextRunTime, 3, 2) As 'nextRunDate' /* Note: the sysjobschedules table refreshes every 20 min, so nextRunDate may be out of date */ , 'Execute msdb.dbo.sp_update_job @job_id = ''' + Cast(job.job_id As char(36)) + ''', @enabled = 0;' As 'disableScript' From msdb.dbo.sysjobs As job Join myCTE As sched On job.job_id = sched.job_id Where job.enabled = 1 -- do not display disabled jobs Order By nextRunDate;
Not sure what I'm doing with the @weekDay table? Then check out my post on bitwise operations in T-SQL.
Happy Coding!
Michelle Ufford (aka SQLFool)
PS: I haven't tested this with every possible setting, just the ones I use. If I missed something, please let me know so I can correct it.
PPS: the sysjobschedules table only refreshes every 20 min, so the nextRunDate value may be a little out of date.
Source: http://sqlfool.com/2009/02/view-sql-agent-jobs-tsql/
Categories
- Business Intelligence
- Internals
- Miscellaneous
- PASS
- Performance & Tuning
- Presentations
- SQL 2008
- SQL Tips
- Syndication
- T-SQL Scripts
Subscribe to my blog!
| Like what you see? Subscribe! |
![]() |
Around the Web
Recent Tweets
- RT @Phil_Factor: SQL Server table columns under the hood http://t.co/vp7gQ77B < what a great post
- @tradney haha that's awesome :)
- OH: @AdamMachanic's #sqlpass session was one of the best I've ever seen.
Archives
- November 2011
- October 2011
- September 2011
- July 2011
- June 2011
- May 2011
- April 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008



February 6th, 2009 - 08:13
Thanks, I need to do exactly this next week you’ve saved me a tonne of time!
February 6th, 2009 - 08:48
You’re welcome, I’m glad I could help!
February 6th, 2009 - 11:58
Nice. Do you know about the Central Management Server feature and group execute in SQL 2008? That way, you can run it once against all of your servers, and get the results back in a single results grid. Very handy for copy/pasting into a spreadsheet.
February 16th, 2009 - 15:43
Great query. Just what I needed for easy documenting the scheduled jobs.
Regards
STefan
May 17th, 2011 - 01:29
Could you tell me, what is reason to disabled jobs ? Reason to use this script ?
New Jr DBA
May 20th, 2011 - 07:20
@Kahn I typically disable jobs whenever we’re about to perform a hardware upgrade or patching.
June 23rd, 2011 - 11:08
Nice script. I can use this for customers which need a lot of hand holding to get information about jobs. Now, I need a script to get information on the details/steps of each job.
Thanks.
June 30th, 2011 - 13:30
I noticed that it doesn’t show all the enabled jobs on my server. Why is that?
July 8th, 2011 - 08:43
I love this script, but I notice that it seems to leave out some jobs that are scheduled and enabled. These jobs show up with “select * From msdb.dbo.sysjobs” and are enabled. I’ve been looking through the script but can’t determine where the problem is.