home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
SQL Server Scripts, Code Samples and SSMS Custom Reports
»
SQL Server 2005/2008
»
List of Upcoming scheduled jobs in SQL Server 2005
List of Upcoming scheduled jobs in SQL Server 2005
Options
Previous Topic
·
Next Topic
Scott Whigham
Posted:
Wednesday, May 09, 2007 4:38:01 PM
Rank: Super Mod
Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
I was migrating a server this week and needed to know what jobs were schedule for the next 24 hours so I wrote this:
Code:
CREATE PROC dbo.GetJobsScheduledInTheNextNHours (@HowManyHoursAhead INT = 24)
AS
/*
Author: Scott Whigham from
http://www.LearnItFirst.com/
Description: This script returns all jobs scheduled to run in the next "N" hours
Misc Notes: Special thanks to whoever runs
http://www.prophecie.co.uk/Default.aspx?dc=200512
as that's where I got the idea for this script!
Versions: SQL Server 2005
Creation Date: May 9, 2007
For more scripts like this one, visit
http://forums.learnsqlserver.com/codesamples.aspx
*/
WITH OurJobs AS (
SELECT job.job_id, job.[name]
, CASE job.[description] WHEN 'No description available.' THEN NULL ELSE job.description END AS Description
, job.date_modified
, CASE sched.next_run_date
WHEN 0 THEN 'Never'
ELSE
CONVERT(varchar(10), CONVERT(smalldatetime, CAST(sched.next_run_date as varchar), 120), 120)+' '+
RIGHT('0'+CAST((sched.next_run_time/10000) AS VARCHAR), 2)+':'+
RIGHT('0'+CAST((sched.next_run_time-((sched.next_run_time/10000)*10000))/100 AS VARCHAR), 2)+':'+
RIGHT('0'+CAST((sched.next_run_time-((sched.next_run_time/10000)*10000)-((sched.next_run_time-((sched.next_run_time/10000)*10000))/100*100)) AS VARCHAR), 2)
END AS NextRunDateTime
, (
SELECT CASE last_run_date
WHEN 0 THEN 'Never'
ELSE
CONVERT(varchar(10), CONVERT(smalldatetime, CAST(last_run_date as varchar), 120), 120)+' '+
RIGHT('0'+CAST((last_run_time/10000) AS VARCHAR), 2)+':'+
RIGHT('0'+CAST((last_run_time-((last_run_time/10000)*10000))/100 AS VARCHAR), 2)+':'+
RIGHT('0'+CAST((last_run_time-((last_run_time/10000)*10000)-((last_run_time-((last_run_time/10000)*10000))/100*100)) AS VARCHAR), 2)
END AS LastRunDateTime
FROM msdb.dbo.sysjobsteps
WHERE job_id = job.job_id AND step_id = (
SELECT MAX(step_id)
FROM msdb.dbo.sysjobsteps
WHERE job_id = job.job_id
)
) as LastSuccessfulExecution
FROM msdb.dbo.sysjobs job JOIN msdb.dbo.sysjobschedules sched
ON sched.job_id = job.job_id
WHERE job.enabled = 1 -- remove this if you wish to return all jobs
AND sched.next_run_date > 0
)
SELECT * FROM OurJobs
WHERE DATEDIFF(hh, GETDATE(), NextRunDateTime) <= @HowManyHoursAhead
GO
EXEC GetJobsScheduledInTheNextNHours 24
Back to top
Scott Whigham
Posted:
Friday, August 14, 2009 12:56:34 PM
Rank: Super Mod
Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
This works great for SQL Server 2008 too!
Back to top
vansree
Posted:
Monday, August 30, 2010 5:48:04 PM
Rank: Newbie
Joined: 8/30/2010
Posts: 1
Points: 3
Where do you live?: Vancouver
Nice script!
Typo: Descripton to Description
Also if possible add order by clause like
SELECT * FROM OurJobs
WHERE DATEDIFF(hh, GETDATE(), NextRunDateTime) <= @HowManyHoursAhead
order by case when Description
like ('This job is owned by a report server process%') then 'zzzzzzz' else Name end
Back to top
Scott Whigham
Posted:
Tuesday, August 31, 2010 7:53:42 AM
Rank: Super Mod
Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
Thanks for the typo fix (corrected)! Yeah, you can order by whatever.
Thanks!
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
You
cannot
post new topics in this forum.
You
cannot
reply to topics in this forum.
You
cannot
delete your posts in this forum.
You
cannot
edit your posts in this forum.
You
cannot
create polls in this forum.
You
cannot
vote in polls in this forum.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Email this topic
RSS Feed
Watch this topic
Print this topic
Normal
Threaded