Solved

How can I get all the information about the jobs I have on a SQL server 2008

Posted on 2010-11-12
7
305 Views
Last Modified: 2012-05-10
I need to create a report with all information about the jobs I have in a SQL server for example:

Job_Name      Schedule                           Owner  .....
Job1               Once a day at 3:00 am      dbo

Please advise me how to created
0
Comment
Question by:hmra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 34121681

Try this:

SELECT j.job_id, j.[name],l.[name] AS 'OwnerName' FROM msdb.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
ORDER BY j.[name] 
 

Open in new window

0
 

Author Comment

by:hmra
ID: 34121721
It looks good but it is missing the schedule time
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 34122029
Ok, here is *everything*, almost.

SELECT j.job_id, j.[name], l.[name] AS 'OwnerName',m.active_start_date, m.active_end_date,m.next_run_date, 
MAX(CAST(
STUFF(STUFF(CAST(jh.run_date as varchar),7,0,'-'),5,0,'-') + ' ' + 
STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime)) AS [LastRun],
CASE jh.run_status WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Success'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
END AS Status
FROM msdb.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
INNER JOIN msdb.dbo.sysjobhistory jh
ON jh.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobschedules m 
ON m.job_id = j.job_id
GROUP BY j.[name],j.job_id, jh.run_status,m.active_start_date, m.active_end_date,m.next_run_date, l.name
ORDER BY j.[name]

Open in new window


You can also add m.active_start_time, m.active_end_time to get job start and end times
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 29

Expert Comment

by:sammySeltzer
ID: 34122046
I suppose you can remove job_id since you didn't ask for it:

SELECT j.[name], l.[name] AS 'OwnerName',m.active_start_date, m.active_end_date,m.next_run_date, 
MAX(CAST(
STUFF(STUFF(CAST(jh.run_date as varchar),7,0,'-'),5,0,'-') + ' ' + 
STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime)) AS [LastRun],
CASE jh.run_status WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Success'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
END AS Status
FROM msdb.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
INNER JOIN msdb.dbo.sysjobhistory jh
ON jh.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobschedules m 
ON m.job_id = j.job_id
GROUP BY j.[name], jh.run_status,m.active_start_date, m.active_end_date,m.next_run_date, l.name
ORDER BY j.[name] 

Open in new window

0
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 34122338
Sorry, I ran the code using our sql 2000 box.

It won't work for 2005 and 2008.

Please try this:

SELECT j.[name], l.[name] AS 'OwnerName',jh.run_date,jh.run_time,jh.run_duration,
MAX(CAST(
STUFF(STUFF(CAST(jh.run_date as varchar),7,0,'-'),5,0,'-') + ' ' + 
STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime)) AS [LastRun], m.next_run_date,
CASE jh.run_status WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Success'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'In progress'
END AS Status
FROM msdb.dbo.sysjobs j
INNER JOIN Master.dbo.syslogins l
ON j.owner_sid = l.sid
INNER JOIN msdb.dbo.sysjobhistory jh
ON jh.job_id = j.job_id
LEFT OUTER JOIN msdb.dbo.sysjobschedules m
ON j.job_id = m.job_id
INNER JOIN msdb.dbo.sysschedules s ON m.schedule_id = s.schedule_id 
LEFT OUTER JOIN (SELECT job_id, max(run_duration) AS run_duration
                FROM msdb.dbo.sysjobhistory jh
                GROUP BY job_id) Q1
ON j.job_id = Q1.job_id

GROUP BY j.[name], jh.run_status,jh.run_date,jh.run_time,jh.run_duration,m.next_run_date, l.name
ORDER BY j.[name] 

Open in new window

0
 

Author Comment

by:hmra
ID: 34122551
We are almost there!!!

active_start_date and active_end_date are not columns of the table msdb.dbo.sysjobschedules m

0
 

Author Comment

by:hmra
ID: 34122637
Whaooo!!!

That is jus what I need thank you very much
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question