MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.
DECLARE @JobToRun NVARCHAR(128) = 'My Agent Job' DECLARE @dtStart DATETIME = GETDATE(), @dtCurr DATETIME DECLARE @ExecutionStatus INT, @LastRunOutcome INT, @MaxTimeExceeded BIT = 0 DECLARE @TimeoutMinutes INT = 180 EXEC msdb.dbo.sp_start_job @JobToRun SET @dtCurr = GETDATE() WHILE 1=1 BEGIN WAITFOR DELAY '00:00:10' SELECT @ExecutionStatus=current_execution_status, @LastRunOutcome=last_run_outcome FROM OPENQUERY(LocalServer, 'set fmtonly off; exec msdb.dbo.sp_help_job') where [name] = @JobToRun IF @ExecutionStatus <> 4 BEGIN -- job is running or finishing (not idle) SET @dtCurr=GETDATE() IF DATEDIFF(mi, @dtStart, @dtCurr) > @TimeoutMinutes BEGIN EXEC msdb.dbo.sp_stop_job @job_name=@JobToRun -- could log info, raise error, send email etc here END ELSE BEGIN CONTINUE END END IF @LastRunOutcome = 1 -- the job just finished with success flag BEGIN -- job succeeded, do whatever is needed here print 'job succeeded' END END
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
|Building JSON Results Table FROM DB||9||38|
|how to extract last 6 digits from a varchar, sql||4||24|
|SQL 2012 Instance Problem||3||62|
|using an update statement with 3 tables but need to get minimum receipt date need help finishings||4||12|