Yeah...I know I know, you could done that using Management Studio. But what if you want to incorporate this function as one part of your batch job? That's why I always say scripting everything you could.
here's how, note depends on the parameter(s) you passed to .sp_help_jobhistory , the order of the columns of the result set are different.
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE SPECIFIC_SCHEMA = N'dbo'
AND SPECIFIC_NAME = N'dbo_mnt_get_latest_job_status'
)
DROP PROCEDURE dbo.dbo_mnt_get_latest_job_status
GO
CREATE PROCEDURE dbo.dbo_mnt_get_latest_job_status
@job_name sysname,
@run_date int = 0,
@run_time int =0
AS
declare @tbl_job_hitory table(
job_id uniqueidentifier,
job_name sysname,
run_status int,
run_date int,
run_time int,
run_duration int,
operator_emailed nvarchar(20),
operator_netsent nvarchar(20),
operator_paged nvarchar(20),
retries_attemped int,
server nvarchar(30))
if @run_date = 0
begin
set @run_date = convert(int,convert(varchar(20), getdate(),112))
end
if @run_time = 0
begin
declare @cur_dt char(8)
set @cur_dt = convert(char(8), getdate(), 108)
set @run_time = substring(@cur_dt,1,2)+substring(@cur_dt,4,2)+substring(@cur_dt,7,2)
end
insert @tbl_job_hitory
exec msdb.dbo.sp_help_jobhistory
@job_name = 'DBS5_PROD_DB_Log_Restore'
,@start_run_time = @run_time,
@start_run_date = @run_date
select job_name,run_date, run_time, run_status from @tbl_job_hitory
where run_date = @run_date and run_time > @run_time
GO
--exec dbo_mnt_get_latest_job_status @job_name=N'DBS5_PROD_DB_Log_Restore',@run_time='190000'