Solved

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

Posted on 2010-11-12
7
306 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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