Need to consolidate Queries sysjobs, sysjobhistory, sysjobschedules

  • I need to modify the first query listed below so that it contains information from the second query.

    The first query list the Job Name, frequency of the job, Interval and time.

    The second query contains job history information Date and time that the job started, completed, duration and Status.

    Any help would be greatly appreciated.

    SELECT

    --'Server' = left(@@ServerName,20),

    'CYP_BI' AS Server,

    --'JobName' = left(S.name,30),

    S.name AS JobName,

    S.description AS Alias,

    --'ScheduleName' = left(ss.name,25),

    --'Enabled' = CASE (S.enabled)

    -- WHEN 0 THEN 'No'

    -- WHEN 1 THEN 'Yes'

    -- ELSE '??'

    -- END,

    'Frequency' = CASE(ss.freq_type)

    WHEN 1 THEN 'Once'

    WHEN 4 THEN 'Daily'

    WHEN 8 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)

    WHEN 16 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)

    WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE

    WHEN 64 THEN 'SQL Startup'

    WHEN 128 THEN 'SQL Idle'

    ELSE '??'

    END,

    'Interval' = CASE

    WHEN (freq_type = 1) then 'One time only'

    WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'

    WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'

    WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7

    from (select ss.schedule_id,

    freq_interval,

    'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,

    'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,

    'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,

    'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,

    'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,

    'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,

    'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END

    from CYP_BI.msdb.dbo.sysschedules ss

    where freq_type = 8

    ) as F

    where schedule_id = sj.schedule_id

    )

    WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)

    WHEN (freq_type = 32) then (select freq_rel + WDAY

    from (select ss.schedule_id,

    'freq_rel' = CASE(freq_relative_interval)

    WHEN 1 then 'First'

    WHEN 2 then 'Second'

    WHEN 4 then 'Third'

    WHEN 8 then 'Fourth'

    WHEN 16 then 'Last'

    ELSE '??'

    END,

    'WDAY' = CASE (freq_interval)

    WHEN 1 then ' Sun'

    WHEN 2 then ' Mon'

    WHEN 3 then ' Tue'

    WHEN 4 then ' Wed'

    WHEN 5 then ' Thu'

    WHEN 6 then ' Fri'

    WHEN 7 then ' Sat'

    WHEN 8 then ' Day'

    WHEN 9 then ' Weekday'

    WHEN 10 then ' Weekend'

    ELSE '??'

    END

    from CYP_BI.msdb.dbo.sysschedules ss

    where ss.freq_type = 32

    ) as WS

    where WS.schedule_id =ss.schedule_id

    )

    END,

    'Time' = CASE (freq_subday_type)

    WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)

    WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'

    WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'

    WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'

    ELSE '??'

    END

    -- ,

    --'Next Run Time' = CASE SJ.next_run_date

    -- WHEN 0 THEN cast('n/a' as char(10))

    -- ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)

    -- END

    FROM CYP_BI.msdb.dbo.sysjobschedules SJ

    join CYP_BI.msdb.dbo.sysjobs S on S.job_id = SJ.job_id

    join CYP_BI.msdb.dbo.sysschedules SS on ss.schedule_id = sj.schedule_id

    WHERE S.name NOT LIKE 'Backup%'

    AND S.Name NOT LIKE 'Cleanup%'

    AND S.Name NOT LIKE 'Shrink%'

    AND S.Name <> 'WMI_Disk_Space_Notification'

    AND S.Name <> 'syspolicy_purge_history'

    AND S.Name NOT LIKE '%Distribution%'

    AND S.Name NOT LIKE '%Replication%'

    AND S.enabled = 1

    --FOR XML RAW, ELEMENTS

    The second query:

    SELECT DISTINCT s.Name AS Job_Name,

    msdb.dbo.Agent_datetime(Run_Date, Run_Time) AS Run_Datetime,

    Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, Run_Duration)), msdb.dbo.Agent_datetime(Run_Date, Run_Time)) AS Run_Endtime,

    Stuff(Stuff(RIGHT('000000'

    + Cast(Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS Run_Duration,

    CASE h.Run_Status

    WHEN 0 THEN 'failed'

    WHEN 1 THEN 'Succeded'

    WHEN 2 THEN 'Retry'

    WHEN 3 THEN 'Cancelled'

    WHEN 4 THEN 'In Progress'

    END AS Execution_Status

    FROM msdb..Sysjobhistory h

    INNER JOIN msdb..Sysjobs s ON h.Job_id = s.Job_id

    WHERE msdb.dbo.Agent_datetime(Run_Date, Run_Time) >= DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))

    AND s.Name NOT LIKE 'Backup%'

    AND s.Name NOT LIKE 'Cleanup%'

    AND s.Name NOT LIKE 'Shrink%'

    AND s.Name <> 'WMI_Disk_Space_Notification'

    AND s.Name <> 'syspolicy_purge_history'

    AND s.Name NOT LIKE '%Distribution%'

    AND s.Name NOT LIKE '%Replication%'

    AND h.Step_ID = 0

    ORDER BY Run_Datetime

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Why not modify the first query to LEFT JOIN to msdb..Sysjobhistory?

  • Care to show us what you have tried to accomplish this task?

  • I tried adding the following from the 1st query to the second query and I get an error invalid column run_date, run_time and run-duration.

    msdb.dbo.Agent_datetime(Run_Date, Run_Time) AS Run_Datetime,

    Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, Run_Duration)), msdb.dbo.Agent_datetime(Run_Date, Run_Time)) AS Run_Endtime,

    Stuff(Stuff(RIGHT('000000' + Cast(Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS Run_Duration,

    I'm missing something in the FROM Clause.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • djj (3/10/2014)


    Why not modify the first query to LEFT JOIN to msdb..Sysjobhistory?

    Thanks.

    I will need a LEFT JOIN on sysjobs because I want them to show up regardless if they have run or not.

    The Jobs do not show up until the job completes. I would like the start time of the jobs that have started but have not yet finished.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Lynn Pettis (3/10/2014)


    Care to show us what you have tried to accomplish this task?

    ok, this is what I have so far and it works. I had to focus on something else that came up.

    SELECT

    'CYP_BI' AS Server,

    S.name AS JobName,

    S.description AS Alias,

    msdb.dbo.Agent_datetime(Run_Date, Run_Time) AS Run_Datetime,

    Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, Run_Duration)), msdb.dbo.Agent_datetime(Run_Date, Run_Time)) AS Run_Endtime,

    Stuff(Stuff(RIGHT('000000'

    + Cast(Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS Run_Duration,

    'ScheduleName' = left(ss.name,25),

    'Enabled' = CASE (S.enabled)

    WHEN 0 THEN 'No'

    WHEN 1 THEN 'Yes'

    ELSE '??'

    END,

    'Frequency' = CASE(ss.freq_type)

    WHEN 1 THEN 'Once'

    WHEN 4 THEN 'Daily'

    WHEN 8 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)

    WHEN 16 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)

    WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE

    WHEN 64 THEN 'SQL Startup'

    WHEN 128 THEN 'SQL Idle'

    ELSE '??'

    END,

    'Interval' = CASE

    WHEN (freq_type = 1) then 'One time only'

    WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'

    WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'

    WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7

    from (select ss.schedule_id,

    freq_interval,

    'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,

    'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,

    'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,

    'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,

    'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,

    'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,

    'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END

    from CYP_BI.msdb.dbo.sysschedules ss

    where freq_type = 8

    ) as F

    where schedule_id = sj.schedule_id

    )

    WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)

    WHEN (freq_type = 32) then (select freq_rel + WDAY

    from (select ss.schedule_id,

    'freq_rel' = CASE(freq_relative_interval)

    WHEN 1 then 'First'

    WHEN 2 then 'Second'

    WHEN 4 then 'Third'

    WHEN 8 then 'Fourth'

    WHEN 16 then 'Last'

    ELSE '??'

    END,

    'WDAY' = CASE (freq_interval)

    WHEN 1 then ' Sun'

    WHEN 2 then ' Mon'

    WHEN 3 then ' Tue'

    WHEN 4 then ' Wed'

    WHEN 5 then ' Thu'

    WHEN 6 then ' Fri'

    WHEN 7 then ' Sat'

    WHEN 8 then ' Day'

    WHEN 9 then ' Weekday'

    WHEN 10 then ' Weekend'

    ELSE '??'

    END

    from CYP_BI.msdb.dbo.sysschedules ss

    where ss.freq_type = 32

    ) as WS

    where WS.schedule_id =ss.schedule_id

    )

    END,

    'Time' = CASE (freq_subday_type)

    WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)

    WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'

    WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'

    WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'

    ELSE '??'

    END

    ,

    'Next Run Time' = CASE SJ.next_run_date

    WHEN 0 THEN cast('n/a' as char(10))

    ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)

    END

    FROM CYP_BI.msdb.dbo.sysjobs S

    LEFT JOIN CYP_BI.msdb.dbo.sysjobschedules SJ on S.job_id = SJ.job_id

    join CYP_BI.msdb.dbo.sysschedules SS on ss.schedule_id = sj.schedule_id

    JOIN CYP_BI.msdb.dbo.sysjobHistory h ON h.Job_id = s.Job_id

    FOR XML RAW, ELEMENTS

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • If the value stop_execution_date is null in msdb.dbo.sysjobactivity the job is running.

  • Thank you!

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I need to revise the following query do display an jobs that are currently running.

    What would be the best way to do this?

    -- SELECT *

    --FROM msdb.dbo.sysjobactivity

    SELECT DISTINCT

    'CYP_BI' AS Server,

    S.name AS JobName,

    S.description AS Alias,

    msdb.dbo.Agent_datetime(Run_Date, Run_Time) AS Run_Datetime,

    Dateadd(second, Datediff(second, 0, msdb.dbo.Agent_datetime(19000101, Run_Duration)), msdb.dbo.Agent_datetime(Run_Date, Run_Time)) AS Run_Endtime,

    Stuff(Stuff(RIGHT('000000'

    + Cast(Run_Duration AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS Run_Duration,

    'ScheduleName' = left(ss.name,25),

    'Enabled' = CASE (S.enabled)

    WHEN 0 THEN 'No'

    WHEN 1 THEN 'Yes'

    ELSE '??'

    END,

    'Frequency' = CASE(ss.freq_type)

    WHEN 1 THEN 'Once'

    WHEN 4 THEN 'Daily'

    WHEN 8 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)

    WHEN 16 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)

    WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE

    WHEN 64 THEN 'SQL Startup'

    WHEN 128 THEN 'SQL Idle'

    ELSE '??'

    END,

    'Interval' = CASE

    WHEN (freq_type = 1) then 'One time only'

    WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'

    WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'

    WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7

    from (select ss.schedule_id,

    freq_interval,

    'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,

    'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,

    'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,

    'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,

    'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,

    'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,

    'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END

    from CYP_BI.msdb.dbo.sysschedules ss

    where freq_type = 8

    ) as F

    where schedule_id = sj.schedule_id

    )

    WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)

    WHEN (freq_type = 32) then (select freq_rel + WDAY

    from (select ss.schedule_id,

    'freq_rel' = CASE(freq_relative_interval)

    WHEN 1 then 'First'

    WHEN 2 then 'Second'

    WHEN 4 then 'Third'

    WHEN 8 then 'Fourth'

    WHEN 16 then 'Last'

    ELSE '??'

    END,

    'WDAY' = CASE (freq_interval)

    WHEN 1 then ' Sun'

    WHEN 2 then ' Mon'

    WHEN 3 then ' Tue'

    WHEN 4 then ' Wed'

    WHEN 5 then ' Thu'

    WHEN 6 then ' Fri'

    WHEN 7 then ' Sat'

    WHEN 8 then ' Day'

    WHEN 9 then ' Weekday'

    WHEN 10 then ' Weekend'

    ELSE '??'

    END

    from CYP_BI.msdb.dbo.sysschedules ss

    where ss.freq_type = 32

    ) as WS

    where WS.schedule_id =ss.schedule_id

    )

    END,

    'Time' = CASE (freq_subday_type)

    WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)

    WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'

    WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'

    WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'

    ELSE '??'

    END

    ,

    'Next Run Time' = CASE SJ.next_run_date

    WHEN 0 THEN cast('n/a' as char(10))

    ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)

    END,

    CASE h.Run_Status

    WHEN 0 THEN 'failed'

    WHEN 1 THEN 'Succeded'

    WHEN 2 THEN 'Retry'

    WHEN 3 THEN 'Cancelled'

    WHEN 4 THEN 'In Progress'

    END AS Status

    FROM CYP_BI.msdb.dbo.sysjobs S

    LEFT JOIN CYP_BI.msdb.dbo.sysjobschedules SJ on S.job_id = SJ.job_id

    join CYP_BI.msdb.dbo.sysschedules SS on ss.schedule_id = sj.schedule_id

    JOIN CYP_BI.msdb.dbo.sysjobHistory h ON h.Job_id = s.Job_id

    WHERE msdb.dbo.Agent_datetime(Run_Date, Run_Time) >= DATEADD(dd,DATEDIFF(dd,0,GETDATE())-1,CAST('18:00' AS DATETIME))

    AND s.Name NOT LIKE 'Backup%'

    AND S.Name NOT LIKE 'Cleanup%'

    AND S.Name NOT LIKE 'Shrink%'

    AND S.Name <> 'WMI_Disk_Space_Notification'

    AND S.Name <> 'syspolicy_purge_history'

    AND S.Name NOT LIKE '%Distribution%'

    AND S.Name NOT LIKE '%Replication%'

    AND S.enabled = 1

    AND h.Step_ID = 0

    ORDER BY Run_Datetime

    --FOR XML RAW, ELEMENTS

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply