Solved

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

Posted on 2010-11-12
7
301 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
  • 4
  • 3
7 Comments
 
LVL 28

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 28

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 28

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 28

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now