Saturday, January 29, 2011

Script to find the job details and timings

/* script to find the server jobs and their details
This script is for SQL server 2005
Author: Arun S Prasad
Mail    : arprasaad@gmail.com
Developed for thedbadelight.blogspot.com
*/

set nocount on
create table #joblisting(jobname varchar(100), enabled varchar(10), description varchar(100), frequency varchar(200),lastrundate varchar(100),Lastrun_status varchar(100))

declare @name varchar(100)
declare @enabled varchar(100)
declare @description varchar(100)
declare @freq_type varchar(100)
declare @freq_interval varchar(100)
declare @freq_subday_type varchar(100)
declare @freq_subday_interval varchar(100)
declare @freq_relative_interval varchar(100)
declare @freq_recurrence_factor varchar(100)
declare @active_start_date varchar(100)
declare @active_end_date varchar(100)
declare @active_start_time varchar(100)
declare @active_end_time varchar(100)
declare @schedule_description varchar(8000)
declare @rundate varchar(100)
declare @status varchar(10)
declare @jobid varchar(100)

DECLARE job_cursor CURSOR
FOR
select a.[name],
a.enabled,
a.description,
c.freq_type,
c.freq_interval,
c.freq_subday_type,
c.freq_subday_interval,
c.freq_relative_interval,
c.freq_recurrence_factor,
c.active_start_date,
c.active_end_date,
c.active_start_time,
c.active_end_time
from msdb..sysjobs a,
msdb..sysjobschedules b,
msdb..sysschedules c
where a.job_id=b.job_id and b.schedule_id = c.schedule_id

OPEN job_cursor

FETCH NEXT FROM job_cursor
INTO   @name,
  @enabled,
  @description,
  @freq_type,
  @freq_interval,
  @freq_subday_type,
  @freq_subday_interval,
  @freq_relative_interval,
  @freq_recurrence_factor,
  @active_start_date,
  @active_end_date,
  @active_start_time,
  @active_end_time


while @@fetch_status = 0
begin

EXEC msdb.dbo.sp_get_schedule_description
@freq_type = @freq_type,
@freq_interval = @freq_interval,
@freq_subday_type = @freq_subday_type,
@freq_subday_interval = @freq_subday_interval,
@freq_relative_interval = @freq_relative_interval,
@freq_recurrence_factor = @freq_recurrence_factor,
@active_start_date = @active_start_date,
@active_end_date = @active_end_date,
@active_start_time = @active_start_time,
@active_end_time = @active_end_time,
@schedule_description = @schedule_description out

select @jobid = job_id from sysjobs where name = @name
select @status=run_status, @rundate=convert(varchar(30),max(run_date))+' '+convert(varchar(30),max(run_time)) from sysjobhistory where job_id = @jobid group by run_status

insert into #joblisting values
(@name,@enabled,@description,@schedule_description,@rundate,case @status when 1 then 'Success' else 'Failure' end)

FETCH NEXT FROM job_cursor
INTO   @name,
  @enabled,
  @description,
  @freq_type,
  @freq_interval,
  @freq_subday_type,
  @freq_subday_interval,
  @freq_relative_interval,
  @freq_recurrence_factor,
  @active_start_date,
  @active_end_date,
  @active_start_time,
  @active_end_time

end

close job_cursor
deallocate job_cursor
--change line number 108 as select * from #joblisting to get the complete detail of the job. Only our requirement is met by this script.
select jobname as JOBNAME,frequency as SCHEDULE from #joblisting

drop table #joblisting

1 comment:

  1. I have got the more information about the scripting of job details and timings.

    ReplyDelete