November 14, 2012 at 10:51 am
Hi all,
If I have a job that executed twice, how can I, using t-sql, find which one executed last?
Thanks,
Eugene
November 15, 2012 at 6:44 am
Huh? If one job executed twice then asking "which one executed last" does not make a lot of sense since it is the same job that executed both times.
For detailed info about jobs and job history you can look in the tables in the dbo schema in msdb with the prefix sysjob.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
November 15, 2012 at 9:03 am
Not sure why it doesn't make sense to you.
For instance you have a scheduled job that runs at 6PM daily. You have an automated email every morning reporting success/failure.
Then job broke. You made corrections and ran it MANUALLY. Now, the next morning you only want to see the report about automated jobs, not the one manual once.
That's fine, I found a way to do it. I am looking at highest instance for a given job to pick the latest.
November 15, 2012 at 9:11 am
eugene.pipko (11/15/2012)
Not sure why it doesn't make sense to you.For instance you have a scheduled job that runs at 6PM daily. You have an automated email every morning reporting success/failure.
Then job broke. You made corrections and ran it MANUALLY. Now, the next morning you only want to see the report about automated jobs, not the one manual once.
That's fine, I found a way to do it. I am looking at highest instance for a given job to pick the latest.
Remember when posting that others cannot see what you see nor do they know that you have attached special meaning to the word "last" unless you explain it. Your clarification about automated versus manual job executions was excellent. If you had written in your first post what you just wrote in your most recent post you most likely would have received a response within only a few minutes. I am happy you found a path forward!
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
November 15, 2012 at 10:18 am
Not sure if I'm late or not but here is one I have used:
SELECT name,
(SELECT MAX(run_date) AS MaxRunDate
FROM msdb.dbo.sysjobhistory AS B
WHERE (job_id = A.job_id)) AS MaxDate,
(SELECT MAX(run_time) AS MaxRunTime
FROM msdb.dbo.sysjobhistory AS B
WHERE (job_id = A.job_id)) AS MaxTime
FROM msdb.dbo.sysjobs AS A
WHERE (name = 'YourJobName')
November 20, 2012 at 7:11 am
SELECT
sj.name,
so.name,
max(run_date) [LastRunDate]
FROM
msdb.dbo.sysjobs sj
inner join msdb.dbo.sysjobhistory sjh
on sj.job_id =sjh.job_id
inner join msdb.dbo.sysoperators so
on sj.notify_email_operator_id = so.id
GROUP BY
sj.name,
so.name
August 20, 2013 at 2:55 pm
It worked. Thanks for the post.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply