Member of the LearnItFirst.com Video Training Network | LearnSqlServer.com | SQL SSIS Training | SQL Programming Tutorials |
LearnSqlServer.com Forums LearnSqlServer.com
Welcome Guest Search | New Posts | Members | Log In | Register

List of Upcoming scheduled jobs in SQL Server 2005 Options
Scott Whigham
Posted: Wednesday, May 09, 2007 4:38:01 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 345
Points: 748
Location: 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)
AS
/*
    Author: Scott Whigham from http://www.LearnSqlServer.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 Descripton
      , 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
)
SELECT * FROM OurJobs WHERE DATEDIFF(hh, GETDATE(), NextRunDateTime) <= @HowManyHoursAhead
GO
EXEC GetJobsScheduledInTheNextNHours 24
Scott Whigham
Posted: Wednesday, May 09, 2007 4:50:49 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 345
Points: 748
Location: Dallas, TX
I uploaded a Custom Report for SQL Server 2005 SP2's SSMS here!

I love the new Custom Reports in SQL Server 2005's SP2 Smile
Users browsing this topic
Guest


Forum Jump
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.
     
Don't Forget!
LearnItFirst.com
Don't Forget!
LearnExchange.com
 
Home | About Us | Support | Contact Us | Privacy | Site Map | Blogs Blogs Refer a Friend and Get a Free Subscription!
© Copyright 2004-2007 LearnItFirst.com LLC. All rights reserved. All trademarks remain the property of their respective owners.
This site is not affiliated in any way with the Microsoft Corporation.