Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

SQL Job Timeline (Graphical) Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2013 9:34 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 11, 2014 4:15 AM
Points: 28, Visits: 448
Some people report errors downloading the EXCEL file with Internet Explorer, please try another browser (chrome of Firefox).

I suspect yahoo hosting does something weird when IE downloads.

Or try this link: https://dl.dropbox.com/u/43939704/MS_SQL_JOB_timeline.xlsx

Theo :)
Post #1413147
Posted Tuesday, January 29, 2013 9:49 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 338, Visits: 1,421
Thank you, it's an excellent script and combined with the spreadsheet is a great way to check your scheduled jobs as a gantt chart rotated by 90 degrees.
Post #1413159
Posted Thursday, January 31, 2013 9:03 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 2:37 AM
Points: 479, Visits: 297
This script is amazing! Thank you Theo!

I found it didn't run on SQL 2000,
so I half-ported it and added a few performance/aesthetic tweaks
and an option to only show times where 2 or more jobs were running.

Thanks again!

--************************
-- This script returns a (graphical) timeline for all SQL jobs
--************************

--Pre Cleanup
IF OBJECT_ID('tempdb..#Timeline') IS NOT NULL DROP TABLE #Timeline;
IF OBJECT_ID('tempdb..#JobRuntime') IS NOT NULL DROP TABLE #JobRuntime;
IF OBJECT_ID('tempdb..#Pivot') IS NOT NULL DROP TABLE #Pivot;
IF OBJECT_ID('tempdb..#JobNames') IS NOT NULL DROP TABLE #JobNames;
GO

SET NOCOUNT ON

DECLARE @Minutes TABLE (DT DATETIME)
DECLARE @Minutes2 TABLE (col2set VARCHAR(250) NOT NULL PRIMARY KEY)
DECLARE
@DT DATETIME, @StartDT DATETIME, @EndDT DATETIME,
@Resolution INT, @RemoveNonactiveJobs INT, @IgnoreDisabledJobs INT, @ShowJobCollisionsOnly INT

SET @StartDT = getdate() - 8 --how far back to look, in days
SET @EndDT = getdate()
SET @Resolution = 15 -- Enter the Resolution in minutes
SET @RemoveNonActiveJobs = 1
SET @IgnoreDisabledJobs = 1
SET @ShowJobCollisionsOnly = 1 --only brings back times when 2 or more jobs were running (set to -1 if you want to show the no-jobs-running timeslots)

--***************************
-- Make a Jobname table
--***************************
SELECT DISTINCT
replace(NAME,',', '') AS JobName --Replace commas (they mess up @cols variable used lower in the code)
INTO #JobNames
FROM msdb.dbo.sysjobs
WHERE enabled = @IgnoreDisabledJobs

--***************************
-- Generate a Datetime table between StartDT and EndDT with x minute Resolution
--***************************
SET @DT = @StartDT

WHILE @DT <= @EndDT
BEGIN
INSERT INTO @Minutes (DT) VALUES (@DT)

SET @DT = dateadd(mi, @Resolution, @DT)
END

--***************************
-- Create a timeline table by crossjoining the Datetime and Jobnames tables
--***************************
SELECT DT,
JobName,
0 AS Active
INTO #Timeline
FROM @Minutes
CROSS JOIN #JobNames

--***************************
-- Create the Job Runtime information table
--***************************
SELECT
replace(NAME,',', '') AS NAME, --Replace commas (they mess up @cols variable used lower in the code)
CAST(
SUBSTRING(CAST(run_date as varchar(8)),1,4)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),5,2)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),7,2)+' '+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),1,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),3,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),5,2)
as datetime)'SDT',
CAST(
SUBSTRING(CAST(run_date as varchar(8)),1,4)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),5,2)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),7,2)+' '+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),1,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),3,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),5,2)
as datetime)+
CAST(
'1900-01-'+
SUBSTRING(RIGHT('00'+CAST(run_duration/10000/24 +1 as varchar(4)),2),1,2)+' '+
SUBSTRING(RIGHT('00'+CAST(run_duration/10000%24 as varchar(4)),2),1,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_duration as varchar(6)),6),3,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_duration as varchar(6)),6),5,2)
as datetime)
'EDT'
INTO #JobRuntime
FROM msdb.dbo.sysjobs job
INNER JOIN msdb.dbo.sysjobhistory his ON his.job_id = job.job_id
AND step_id = 0 --only gives back Job history, not Step history
AND job.NAME NOT IN ('Database Mirroring Monitor Job','<dbname>')
AND
CAST(
SUBSTRING(CAST(run_date as varchar(8)),1,4)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),5,2)+'-'+
SUBSTRING(CAST(run_date as varchar(8)),7,2)+' '+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),1,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),3,2)+':'+
SUBSTRING(RIGHT('000000'+CAST(run_time as varchar(6)),6),5,2)
as datetime) BETWEEN @StartDT AND @EndDT
ORDER BY SDT

--***************************
-- Update the Timeline based on the the Job Runtime information table
--***************************
UPDATE #Timeline
SET Active = 1
FROM #Timeline
INNER JOIN #JobRuntime ON JobName = NAME
AND(SDT BETWEEN dt AND dateadd(mi, @Resolution - 1, DT) -- Start point (added for Resolution support)
OR EDT BETWEEN dt AND dateadd(mi, @Resolution, DT) -- End point (added for Resolution support)
OR DT BETWEEN SDT AND EDT
)

--***************************
-- Delete all jobs from the Timeline that that had no activity
--***************************
IF @RemoveNonactiveJobs = 1
DELETE
FROM #Timeline
WHERE JobName NOT IN(
SELECT Jobname
FROM #Timeline
WHERE active = 1
GROUP BY Jobname
)

--***************************
-- SQL 2000: Show Job Run Details
--***************************
IF @@VERSION LIKE '%SQL Server 2000%'
SELECT DISTINCT
counts.DT,
counts.TotalRunningJobs,
t.JobName
FROM #Timeline t
INNER JOIN
(SELECT COUNT(DISTINCT JobName)'TotalRunningJobs',DT
FROM #Timeline
WHERE active=1
GROUP BY DT
HAVING COUNT(DISTINCT JobName) > @ShowJobCollisionsOnly
)counts ON t.DT=counts.DT AND t.active=1
ORDER BY 1 DESC,2 DESC,3

--***************************
-- SQL 2005 and later: Pivot the Timeline table
--***************************
IF @@VERSION NOT LIKE '%SQL Server 2000%'
BEGIN
CREATE TABLE #Pivot (
col1 VARCHAR(250) NULL,
col2 VARCHAR(250) NULL,
col3 INT NULL
)

-- col1 = row, col2 = column, col3 = data
INSERT INTO #Pivot
SELECT DISTINCT
convert(VARCHAR(250), DT, 120) DT,
JobName,
Active
FROM #Timeline

-- Make a table with all unique col2 values
INSERT INTO @Minutes2
SELECT DISTINCT col2
FROM #Pivot

--SELECT * FROM #Pivot --debug check

DECLARE @cols AS NVARCHAR(MAX);SET @cols = '' --in SQL 2000, change MAX to 4000
DECLARE @sql AS NVARCHAR(MAX);SET @sql = '' --in SQL 2000, change MAX to 4000

SELECT @cols = @cols +
CASE
WHEN @cols = '' THEN ''
ELSE ',' + CHAR(10) --add comma and a newline
END
+ quotename(col2set)
FROM @Minutes2
GROUP BY col2set
ORDER BY col2set

--PRINT @cols --debug option

-- Build the pivot statement as a dynamic sql statement
SET @sql = N'
SELECT
col1 as Date,
' + REPLACE(@cols,',','+') + N'as TotalRunningJobs,' +
@cols + N'
FROM
(SELECT *
FROM #Pivot
)AS D
PIVOT
(MAX(col3)
FOR col2 IN(' +@cols + N')
)AS P
WHERE ' +REPLACE(@cols,',','+') + ' > '+CAST(@ShowJobCollisionsOnly as nvarchar(10))+'
ORDER BY 1'

--PRINT @sql --debug option

--***************************
-- Output the Timeline table
--***************************
EXEC sp_executesql @sql
END --of the SQL 2005+ IF statement

--Post Cleanup
IF OBJECT_ID('tempdb..#Timeline') IS NOT NULL DROP TABLE #Timeline;
IF OBJECT_ID('tempdb..#JobRuntime') IS NOT NULL DROP TABLE #JobRuntime;
IF OBJECT_ID('tempdb..#Pivot') IS NOT NULL DROP TABLE #Pivot;
IF OBJECT_ID('tempdb..#JobNames') IS NOT NULL DROP TABLE #JobNames;

Post #1414190
Posted Thursday, January 31, 2013 10:16 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 11, 2014 4:15 AM
Points: 28, Visits: 448
Ahhh...

This is why i like SqlCentral so much, its truly is a place where ideas can be shared

Thanks for posting the tweaked version!

Theo
Post #1414249
Posted Monday, February 11, 2013 10:54 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
Theo,

It's really good script. i ran it and it works.
but i have one issue. After getting the result for script, i paste data in excel.
In excel it didn't show lob name. Instead of these it, it show 0.

Thanks
Vinay




Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1418754
Posted Tuesday, February 12, 2013 2:02 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 2:37 AM
Points: 479, Visits: 297
Hi Vinay,

the trick is to right-click on the results and select "Copy with Headers".
That copies the column names too.
Post #1418801
Posted Thursday, May 9, 2013 10:11 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 12:28 PM
Points: 52, Visits: 446
This script is great!!! It's something I've wanted but even beginning it was way over my head. I assume you are using conditional formatting on the cell color in Excel? Brilliant!

Instead of the previous 24 hours, do you have anything that looks at what's sheduled and coming up?

Thanks!

--@ The Wiz, when I run your script, it does not give each time increment, only the records where the job ran. So when I put it in Excel, I don't get the graph I get when the orig. script is pasted in with (all 0s and 1s).
Post #1451244
Posted Friday, May 10, 2013 3:09 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 2:37 AM
Points: 479, Visits: 297
Hi Lisa!

If you really want to see all the time-slots, not just the "interesting ones" (where 1 or more jobs are running),
then grab the now-fixed script from above and SET @ShowJobCollisionsOnly = -1

I took out the no-jobs-running timeslots to cut down on the data the script returns, to make it easier for people to scan the "important" bits, but I guess should have left the option to display them in there for people who wanted it
Post #1451480
Posted Friday, May 24, 2013 4:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 18, 2014 8:05 AM
Points: 1, Visits: 32
Hello,
thank you, great skript.
i changed time(0) and cast to convert(DATETIME

then it worked for me (sql 2005)

regards


Post #1456380
Posted Friday, June 14, 2013 3:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, January 17, 2014 9:12 AM
Points: 4, Visits: 44
This is a fantastic script but where i work i have 2 server still running SQL Server 2000. I have tried and tried to get the information to display the same way but just cant seem to get it to work.

Can anyone help me out and get the information to display the same way as it does with the original script?
Post #1463442
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse