How to get the last run job details in SQL Server Agent using SQL including the step details only for the last run job (not the job outcome) as i want to display this in an application
Please help been stuck on this for ages
This is the code I have been using below, this brings back all steps for all Jobs in the Job History,
However, I just want to see the steps from the last run job
thanks
USE msdb
Go
SELECT j.name JobName,h.step_name StepName,
CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS dateTIME), 111) RunDate,
STUFF(STUFF(RIGHT('000000' + CAST ( h.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunTime,
h.run_duration StepDuration,
case h.run_status when 0 then 'Failed'
when 1 then 'Succeeded'
when 2 then 'Retry'
when 3 then 'Cancelled'
when 4 then 'In Progress'
end as ExecutionStatus,
h.message MessageGenerated
FROM sysjobhistory h
inner join sysjobs j
ON j.job_id = h.job_id
LEFT JOIN (
SELECT
[job_id]
, [run_date]
, [run_time]
, [run_status]
, [run_duration]
, [message]
, ROW_NUMBER() OVER (
PARTITION BY [job_id]
ORDER BY [run_date] DESC, [run_time] DESC
) AS RowNumber
FROM [msdb].[dbo].[sysjobhistory]
WHERE [step_id] = 0
) AS [sJOBH]
ON j.[job_id] = [sJOBH].[job_id]
AND [sJOBH].[RowNumber] = 1
where j.job_id = 'F04E5D3B-C873-448A-805C-C6309A92DAEC'
ORDER BY j.name, h.run_date, h.run_time desc
GO
Hopefully this helps,
Additional join to msdb.dbo.sysjobactivity shows you the start/end time at a job level. Using this range you can specify to only show you the job steps for the most recent run.
DECLARE #job_id UNIQUEIDENTIFIER
,#job_name VARCHAR(256)
SET #job_id = 'DF4C9555-5B24-4649-97CE-5708C53F762C'
SET #job_name = 'syspolicy_purge_history'
--search for job_id if none was provided
SELECT #job_id = COALESCE(#job_id,job_id)
FROM msdb.dbo.sysjobs
WHERE name = #job_name
SELECT t2.instance_id
,t1.name as JobName
,t2.step_id as StepID
,t2.step_name as StepName
,CONVERT(CHAR(10), CAST(STR(t2.run_date,8, 0) AS DATETIME), 111) as RunDate
,STUFF(STUFF(RIGHT('000000' + CAST ( t2.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') as RunTime
,t2.run_duration
,CASE t2.run_status WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Cancelled'
WHEN 4 THEN 'In Progress'
END as ExecutionStatus
,t2.message as MessageGenerated
FROM msdb.dbo.sysjobs t1
JOIN msdb.dbo.sysjobhistory t2
ON t1.job_id = t2.job_id
--Join to pull most recent job activity per job, not job step
JOIN (
SELECT TOP 1
t1.job_id
,t1.start_execution_date
,t1.stop_execution_date
FROM msdb.dbo.sysjobactivity t1
--If no job_id detected, return last run job
WHERE t1.job_id = COALESCE(#job_id,t1.job_id)
ORDER
BY last_executed_step_date DESC
) t3
--Filter on the most recent job_id
ON t1.job_id = t3.job_Id
--Filter out job steps that do not fall between start_execution_date and stop_execution_date
AND CONVERT(DATETIME, CONVERT(CHAR(8), t2.run_date, 112) + ' '
+ STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), t2.run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)
BETWEEN t3.start_execution_date AND t3.stop_execution_date
Edit: Added two parameters to the top, #job_id and #job_name. If #job_id is not provided, first it'll attempt to determine the job_id of the #job_name provided, and return the last run job is no values are provided.
Related
I have N jobs with M steps in each, that could change any time(Add/delete some steps). Jobs have different schedule to run. I want a query to get last run status, job id, job name of each job. Is it right way to do this scenario by (Job outcome) ?
This is the query I use (a little tuned) to check job status. This variation will select the last job outcome for each job.
SELECT
JobName = J.name,
H.*
FROM
msdb.dbo.sysjobs AS J
CROSS APPLY (
SELECT TOP 1
JobName = J.name,
StepNumber = T.step_id,
StepName = T.step_name,
StepStatus = CASE T.run_status
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
ELSE 'Running' END,
ExecutedAt = msdb.dbo.agent_datetime(T.run_date, T.run_time),
ExecutingHours = ((T.run_duration/10000 * 3600 + (T.run_duration/100) % 100 * 60 + T.run_duration % 100 + 31 ) / 60) / 60,
ExecutingMinutes = ((T.run_duration/10000 * 3600 + (T.run_duration/100) % 100 * 60 + T.run_duration % 100 + 31 ) / 60) % 60,
Message = T.message
FROM
msdb.dbo.sysjobhistory AS T
WHERE
T.job_id = J.job_id
ORDER BY
T.instance_id DESC) AS H
ORDER BY
J.name
If you change the TOP 1 for TOP 2, then you will also see the last step executed, aside from the job outcome.
You can not get everything in a single query and need more research. For e.g., to know Job execution information you can try the following query.
SELECT
[sJOB].[job_id] AS [JobID]
, [sJOB].[name] AS [JobName]
, CASE
WHEN [sJOBH].[run_date] IS NULL OR [sJOBH].[run_time] IS NULL THEN NULL
ELSE CAST(
CAST([sJOBH].[run_date] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJOBH].[run_time] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END AS [LastRunDateTime]
, CASE [sJOBH].[run_status]
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Succeeded'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Canceled'
WHEN 4 THEN 'Running' -- In Progress
END AS [LastRunStatus]
, STUFF(
STUFF(RIGHT('000000' + CAST([sJOBH].[run_duration] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS [LastRunDuration (HH:MM:SS)]
, [sJOBH].[message] AS [LastRunStatusMessage]
, CASE [sJOBSCH].[NextRunDate]
WHEN 0 THEN NULL
ELSE CAST(
CAST([sJOBSCH].[NextRunDate] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJOBSCH].[NextRunTime] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END AS [NextRunDateTime]
FROM
[msdb].[dbo].[sysjobs] AS [sJOB]
LEFT JOIN (
SELECT
[job_id]
, MIN([next_run_date]) AS [NextRunDate]
, MIN([next_run_time]) AS [NextRunTime]
FROM [msdb].[dbo].[sysjobschedules]
GROUP BY [job_id]
) AS [sJOBSCH]
ON [sJOB].[job_id] = [sJOBSCH].[job_id]
LEFT JOIN (
SELECT
[job_id]
, [run_date]
, [run_time]
, [run_status]
, [run_duration]
, [message]
, ROW_NUMBER() OVER (
PARTITION BY [job_id]
ORDER BY [run_date] DESC, [run_time] DESC
) AS RowNumber
FROM [msdb].[dbo].[sysjobhistory]
WHERE [step_id] = 0
) AS [sJOBH]
ON [sJOB].[job_id] = [sJOBH].[job_id]
AND [sJOBH].[RowNumber] = 1
ORDER BY [JobName]
You can get and learn in details - Here and Here
I'm having following table and below is expected results. Please let me known if there is a easy to way to get the expected results in SQL server.
EmpNo Name Benefit StartDate Status
--------------------------------------------
0001 ABC Medical 01/01/2014 Active
0001 ABC Dental 02/02/2013 Inactive
0001 ABC Vision 03/03/2012 Active
0002 XYZ Medical 01/01/2014 Active
0002 XYZ Dental 02/02/2008 Inactive
The results should be like below
EmpNo Name MedicalStart MedStatus DenStart DenStatus VisionStart VisStatus
---------------------------------------------------------------------------------------
0001 ABC 01/01/2014 Active 02/02/2013 Inactive 03/03/2012 Active
0002 XYZ 01/01/2014 Active 02/02/2008 Inactive .
I forgot put a few notes in my initial post.
1) There are 10 benefit plans available, so an employee may enroll for any number of plans up to ten (all plan or few plan or no plans at all).
2) There will be only one row with same benefit plan per EmpNo/Name.
3) Also, there are several fields associated with each row, for example, election option (Self, Family, etc) and many more. To make it simple, I have not included in the question.
Sample data:
CREATE TABLE #Test
(
EmpNo INT
, Name VARCHAR(255)
, Benefit VARCHAR(255)
, StartDate DATETIME2
, Status VARCHAR(255)
);
INSERT INTO #Test
(EmpNo, Name, Benefit, StartDate, Status)
VALUES
(0001, 'ABC', 'Medical', '01/01/2014', 'Active')
, (0001, 'ABC', 'Dental', '02/02/2013', 'Inactive')
, (0001, 'ABC', 'Vision', '03/03/2012', 'Active')
, (0002, 'XYZ', 'Medical', '01/01/2014', 'Active')
, (0002, 'XYZ', 'Dental', '02/02/2008', 'Inactive')
And a simple group clause:
Actual query (if there are historical records), using ROW_NUMBER will let you find latest record for each User and its Benefit:
SELECT T.EmpNo
, T.Name
, MAX(CASE WHEN T.Benefit = 'Medical ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS MedStart
, MAX(CASE WHEN T.Benefit = 'Medical' THEN T.Status END) AS MedStatus
, MAX(CASE WHEN T.Benefit = 'Dental ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS DenStart
, MAX(CASE WHEN T.Benefit = 'Dental' THEN T.Status END) AS DenStatus
, MAX(CASE WHEN T.Benefit = 'Vision ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS VisStart
, MAX(CASE WHEN T.Benefit = 'Vision' THEN T.Status END) AS VisStatus
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY EmpNo, Name, Benefit ORDER BY StartDate DESC) AS RowNo
, EmpNo
, Benefit
, Name
, StartDate
, Status
FROM #Test
) AS T
WHERE T.RowNo = 1
GROUP BY T.EmpNo
, T.Name
Query using dynamic SQL if there is unknown amount of Benefits. Might not be very efficient:
DECLARE #SQL NVARCHAR(MAX) = 'SELECT T.EmpNo, T.Name'
, #Benefit VARCHAR(MAX);
SELECT #SQL += ', MAX(CASE WHEN T.Benefit = ''' + Benefit + ''' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS ' + LEFT(Benefit, 3) + 'Star
, MAX(CASE WHEN T.Benefit = ''' + Benefit + ''' THEN T.Status END) AS ' + LEFT(Benefit, 3) + 'Status'
FROM (SELECT DISTINCT Benefit FROM #Test) AS T
SET #SQL += '
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY EmpNo, Name, Benefit ORDER BY StartDate DESC) AS RowNo, EmpNo, Benefit, NAME, StartDate, STATUS
FROM #Test
) AS T
WHERE T.RowNo = 1
GROUP BY T.EmpNo, T.Name'
EXEC sp_executesql #SQL
Query (if there are no historical records):
SELECT T.EmpNo
, T.Name
, MAX(CASE WHEN T.Benefit = 'Medical ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS MedStart
, MAX(CASE WHEN T.Benefit = 'Medical' THEN T.Status END) AS MedStatus
, MAX(CASE WHEN T.Benefit = 'Dental ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS DenStart
, MAX(CASE WHEN T.Benefit = 'Dental' THEN T.Status END) AS DenStatus
, MAX(CASE WHEN T.Benefit = 'Vision ' THEN CONVERT(VARCHAR(10), CONVERT(DATE, T.StartDate, 106), 103) END) AS VisStart
, MAX(CASE WHEN T.Benefit = 'Vision' THEN T.Status END) AS VisStatus
FROM #Test AS T
GROUP BY T.EmpNo
, T.Name
Output:
EmpNo Name MedStart MedStatus DenStart DenStatus VisStart VisStatus
-------------------------------------------------------------------------------------
1 ABC 01/01/2014 Active 02/02/2013 Inactive 03/03/2012 Active
2 XYZ 01/01/2014 Active 02/02/2008 Inactive NULL NULL
PIVOT solution on StartDate field:
DECLARE #tb AS TABLE
(
EmpNo INT
,Name NVARCHAR(25)
,Benefit NVARCHAR(25)
,StartDate DATE
,[Status] NVARCHAR(25)
);
INSERT INTO #tb VALUES (1, 'ABC', 'Medical', '01/01/2014', 'Active');
INSERT INTO #tb VALUES (1, 'ABC', 'Dental', '02/02/2013', 'Inactive');
INSERT INTO #tb VALUES (1, 'ABC', 'Vision', '03/03/2012', 'Active');
INSERT INTO #tb VALUES (2, 'XYZ', 'Medical', '01/01/2014', 'Active');
INSERT INTO #tb VALUES (2, 'XYZ', 'Dental', '02/02/2012', 'Inactive');
SELECT EmpNo
,Name
,MAX(MedicalStart) AS MedicalStart
,MAX(MedStatus) AS MedStatus
,MAX(DenStart) AS DenStart
,MAX(DenStatus) AS DenStatus
,MAX(VisionStart) AS VisionStart
,MAX(VisStatus) AS VisStatus
FROM
(
SELECT EmpNo
,Name
,[Medical] AS MedicalStart
,CASE
WHEN [Medical] IS NOT NULL AND [Status] = 'Active' THEN 'Active'
WHEN [Medical] IS NOT NULL AND [Status] = 'Inactive' THEN 'Inactive'
ELSE NULL END AS MedStatus
,[Dental] AS DenStart
,CASE
WHEN [Dental] IS NOT NULL AND [Status] = 'Active' THEN 'Active'
WHEN [Dental] IS NOT NULL AND [Status] = 'Inactive' THEN 'Inactive'
ELSE NULL END AS DenStatus
,[Vision] AS VisionStart
,CASE
WHEN [Vision] IS NOT NULL AND [Status] = 'Active' THEN 'Active'
WHEN [Vision] IS NOT NULL AND [Status] = 'Inactive' THEN 'Inactive'
ELSE NULL END AS VisStatus
,[Status]
FROM #tb
PIVOT
(
MAX(StartDate)
FOR Benefit IN ([Medical], [Dental], [Vision])
) AS pivotTableDate
) AS tb
GROUP BY EmpNo, Name;
You can check this link 'PIVOT on two or more fields in SQL Server' for information about full PIVOT solution.
I assume that for each employee you have at most 1 row for Medical, at most 1 row for Dental, and at most 1 row for Vision. If so, you can do it like this:
select
t.EmpNo, t.Name,
tMedical.MedicalStart, tMedical.MedicalStatus
from
(
select
EmpNo, Name
from
TableName
group by EmpNo, Name
) t
left outer join
(
select
EmpNo, Name, Benefit,
min(StartDate) as MedicalStart,
min(Status) as MedicalStatus
from
TableName
where
Benefit = 'Medical'
group by EmpNo, Name, Benefit
) tMedical on t.EmpNo = tMedical.EmpNo and t.Name = tMedical.Name
left outer join ...
Analogically to tMedical, you can add here left joins to tDental and tVision. And that should be it.
You can use Outer Apply to do this. I am assuming that one employee can have any number of medical, dental and vision rows. This query will take the latest StartDate for each type.
Select EmpNo, Name, Medical.StartDate MedicalStart, Medical.Status MedStatus, Dental.StartDate DenStart, Dental.Status DenStatus, Vision.StartDate VisionStart, Vision.Status VisStatus
From (Select Distinct EmpNo, Name From TableName) Emp
Outer Apply (Select Top 1 StartDate, Status From TableName Med Where Benefit='Medical' and Med.EmpNo=Emp.EmpNo Order By StartDate Desc) Medical
Outer Apply (Select Top 1 StartDate, Status From TableName Den Where Benefit='Dental' and Den.EmpNo=Emp.EmpNo Order By StartDate Desc) Dental
Outer Apply (Select Top 1 StartDate, Status From TableName Vis Where Benefit='Vision' and Vis.EmpNo=Emp.EmpNo Order By StartDate Desc) Vision
Let me know if this works for you...
I have a SP that returns four different result sets, but for it to be used in SSRS report i need to have it configured in a proper way.
USE LearnShare
GO
IF OBJECT_ID (N'dbo.JH_LearnShare_ActiveUsers', N'P') IS NOT NULL
DROP PROCEDURE dbo.JH_LearnShare_ActiveUsers
GO
CREATE PROCEDURE dbo.JH_LearnShare_ActiveUsers
#enddate as date
AS
Begin
--Declare #enddate as date
--set #enddate = '6/30/2014'
Select COUNT (distinct p.PersonID) as [Total Active Users] --50621
FROM Enrollment E inner join
Person p on p.PersonID = e.[Person ID]
WHERE p.active = 1
and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
and [Completion Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
--and convert(varchar(10),[Enrollment Date] , 120) < convert(varchar(10),FirstLaunch , 120) --118669
--and FirstLaunch <> '1900-01-01 00:00:00.000' -- this doesn't matter when adding [completion date] in where clause
Select COUNT (distinct p.PersonID) as [Total Active Online Users] --49269
FROM Enrollment E inner join
Person p on p.PersonID = e.[Person ID]
left join class c
on e.[resource id] = c.[resource id]
WHERE p.active = 1
and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
and [Completion Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
and c.[Resource ID] is null
select COUNT (distinct p.personID) as [User with ILT courses] --22656
from Person p
inner join Enrollment e on p.PersonID = e.[Person ID]
inner join Resources r on e.[Resource ID] = r.[Resource ID]
inner join Class c on r.[Resource ID] = c.[Resource ID]
where p.Active = 1
and c.[Class Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
and e.FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
--DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)) AND GETDATE()
and e.[Completion Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
SELECT DateName(yyyy, FirstLaunch) EnrollmentYear, DATENAME(month, FirstLaunch) AS EnrollmentMonth , Count(distinct p.PersonID) as ActiveUsers --190312
FROM Enrollment E inner join
Person p on p.PersonID = e.[Person ID]
WHERE p.active = 1
and FirstLaunch between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
and [Completion Date] between DATEADD(m,-11,DATEADD(mm, DATEDIFF(m,0,#enddate), 0)) AND #enddate
--and convert(varchar(10),[Enrollment Date] , 120) < convert(varchar(10),FirstLaunch , 120) --118669
--and FirstLaunch <> '1900-01-01 00:00:00.000' -- this doesn't matter when adding [completion date] in where clause
AND [Completion Date] <> '1900-01-01 00:00:00.000'
GROUP BY DateName(yyyy,FirstLaunch) , DATENAME(month,FirstLaunch), DATEPART(MONTH,FirstLaunch)
ORDER BY DateName(yyyy,FirstLaunch) desc , DATEPART(MONTH,FirstLaunch) DESC
End
I tried following
Create StoredProcedure sp_MultipleDataSets(#Param nvarchar(10))
as
begin
if(#Param == "first")
begin
SELECT EMPID, ENAME, JOB, SAL, DEPTID FROM EMP -- first result set
end
if(#Param == "second")
begin
SELECT DEPTID, DNAME, LOC FROM DEPT --second result set
end
end
Example adding if (#enddate == "first") begin select..... end but its giving me an error
Msg 102, Level 15, State 1, Procedure JH_LearnShare_ActiveUsers, Line 15
Incorrect syntax near '='.
any help, please. Thanks!
I know this is late but maybe this response will help someone finding this later on.
You could write the SP in that way, however it seems unnecessarily complicated. There should be no reason you can't just write 4 different stored procedures. However, if you want to go down that route, that will work. EX:
IF #datasets = 1
begin
select 1
end
IF #datasets = 2
begin
select 2
end
I am writing a query to find currently running job in SQL (I know we can view it in Job Active Monitor, but I've a need to do in TSQL). Though I can query sysjobactivity table to find currently running job, it's nowhere telling what job step is running (because my job might have more than 1 step).
Query I used:
SELECT s.name AS [JOB_NAME],
'' AS [STEP_ID],
'' AS STEP_NAME,
'Processing' AS STATUS,
sja.run_requested_date AS START_TIME,
null AS END_DATE,
convert(varchar, (getdate() - sja.run_requested_date), 8) AS Duration
FROM sysjobactivity sja, sysjobs s
WHERE sja.job_id = s.job_id
AND sja.run_requested_date > getdate() - 1
AND sja.stop_execution_date IS NULL
Please help me finding the step ID & Step name in which the job is currently progressing.
I think below script help to get SQL Jobs with current execution step, try this
msdb.dbo.sp_help_job #execution_status = 1
DECLARE #StepCount INT
SELECT #StepCount = COUNT(1)
FROM msdb.dbo.sysjobsteps
WHERE job_id = '0523333-5C24-1526-8391-AA84749345666' --JobID
SELECT
[JobName]
,[JobStepID]
,[JobStepName]
,[JobStepStatus]
,[RunDateTime]
,[RunDuration]
FROM
(
SELECT
j.[name] AS [JobName]
,Jh.[step_id] AS [JobStepID]
,jh.[step_name] AS [JobStepName]
,CASE
WHEN jh.[run_status] = 0 THEN 'Failed'
WHEN jh.[run_status] = 1 THEN 'Succeeded'
WHEN jh.[run_status] = 2 THEN 'Retry (step only)'
WHEN jh.[run_status] = 3 THEN 'Canceled'
WHEN jh.[run_status] = 4 THEN 'In-progress message'
WHEN jh.[run_status] = 5 THEN 'Unknown'
ELSE 'N/A'
END AS [JobStepStatus]
,msdb.dbo.agent_datetime(run_date, run_time) AS [RunDateTime]
,CAST(jh.[run_duration]/10000 AS VARCHAR) + ':' + CAST(jh.[run_duration]/100%100 AS VARCHAR) + ':' + CAST(jh.[run_duration]%100 AS VARCHAR) AS [RunDuration]
,ROW_NUMBER() OVER
(
PARTITION BY jh.[run_date]
ORDER BY jh.[run_date] DESC, jh.[run_time] DESC
) AS [RowNumber]
FROM
msdb.[dbo].[sysjobhistory] jh
INNER JOIN msdb.[dbo].[sysjobs] j
ON jh.[job_id] = j.[job_id]
WHERE
j.[name] = 'ProcessCubes' --Job Name
AND jh.[step_id] > 0
AND CAST(RTRIM(run_date) AS DATE) = CAST(GETDATE() AS DATE) --Current Date
) A
WHERE
[RowNumber] <= #StepCount
AND [JobStepStatus] = 'Failed'
Try this:
SELECT distinct
cast([sJOB].[job_id] as varchar(max)) AS execution_id
, [sJSTP].[step_name] AS executable_name
, [sJOB].[name] AS package_name
, CASE [sJSTP].[run_date]
WHEN 0 THEN NULL
ELSE
CAST(
CAST([sJSTP].[run_date] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END AS start_time,
dateadd(ss, run_duration, CASE [sJSTP].[run_date]
WHEN 0 THEN NULL
ELSE
CAST(
CAST([sJSTP].[run_date] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END) end_time
-- , [sJSTP].[run_duration] [looptijd in minuten]
, CASE [sJSTP].[run_status]
WHEN 0 THEN 'Failed'
WHEN 1 THEN 'Success'
WHEN 2 THEN 'Retry'
WHEN 3 THEN 'Cancelled'
WHEN 5 THEN 'Unknown'
END AS execution_result_description
FROM
[msdb].[dbo].[sysjobhistory] AS [sJSTP]
INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB]
ON [sJSTP].[job_id] = [sJOB].[job_id]
inner join [msdb].[dbo].[sysjobsteps] steps
ON [sJSTP].[job_id] = [steps].[job_id]
where [sJSTP].[run_date] <> 0
and CASE [sJSTP].[run_date]
WHEN 0 THEN NULL
ELSE
CAST(
CAST([sJSTP].[run_date] AS CHAR(8))
+ ' '
+ STUFF(
STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6)
, 3, 0, ':')
, 6, 0, ':')
AS DATETIME)
END between dateadd(hh, -20, getdate()) and getdate()
and [sJSTP].[step_name] not in ('(Job outcome)')
order by start_time desc
Additionally I use this query to see step result from a running SSIS job. However, this only shows you the finished steps, not the running ones. I still have to find an SQL to see the currently running step and merge it with this one.
select distinct
cast(e.execution_id as varchar(max)),
e.executable_name,
e.package_name,
CONVERT(datetime, es.start_time) AS start_time
, CONVERT(datetime, es.end_time) AS end_time
, datediff(mi, es.start_time, es.end_time) [running time]
, case es.execution_result
when 0 then 'Success'
when 1 then 'Failed'
when 2 then 'Completion'
when 3 then 'Cancelled'
else cast(es.execution_result as varchar(max)) end as execution_result_description
from ssisdb.catalog.executables e
left join ssisdb.catalog.executable_statistics es
on e.executable_id = es.executable_id
and e.execution_id = es.execution_id
order by 6 desc
Is is possible to list all the scheduled jobs that have been executed for a specified date? I'm having a problem where I am not sure if the job has executed yesterday and if all the steps where executed as well?
To list all the jobs that started within a specified date:
declare #date date = getdate()
SELECT
J.job_id,
J.name
FROM msdb.dbo.sysjobs AS J
INNER JOIN msdb.dbo.sysjobhistory AS H ON H.job_id = J.job_id
WHERE run_date = CONVERT(VARCHAR(8), GETDATE(), 112)
GROUP BY J.job_id, J.name
To list all the steps for a specified job on a specified date with their status:
declare #date date = getdate()
declare #job_name varchar(50) = 'test'
SELECT
H.run_date,
H.run_time,
H.step_id,
H.step_name,
H.run_status
FROM msdb.dbo.sysjobs AS J
INNER JOIN msdb.dbo.sysjobhistory AS H ON H.job_id = J.job_id
WHERE
run_date = CONVERT(VARCHAR(8), GETDATE(), 112)
AND J.name = #job_name
More information here.
find in this below link it gives all the information about sql jobs
http://www.mssqltips.com/sqlservertip/2561/querying-sql-server-agent-job-information/