I really had to do this recently, and that is how I am going to implement it. I create a temporary job through sp_add_job and sp_add_jobstep and set @delete_level to 3 (always delete after launch).
I am not 100% selling this approach, as you can probably tell from the name of the stored procedure. However, it works:
CREATE PROCEDURE spWorkaround_checkJobExists @job_id UNIQUEIDENTIFIER , @thisIteration tinyint , @maxRecurse tinyint AS IF (@thisIteration <= @maxRecurse) BEGIN IF EXISTS( select * FROM msdb.dbo.sysjobs where job_id = @job_id ) BEGIN WAITFOR DELAY '00:00:01' DECLARE @nextIteration int SET @nextIteration = @thisIteration + 1 EXEC dbo.spWorkaround_checkJobExists @job_id, @nextIteration, @maxRecurse END END
Of course, you will want to paste the code to ensure the maximum number of times it takes, but you get this idea. You can also pass a parameter to control how often recursion occurs. In my case, after ten seconds, the results are meaningless.
What I do here can be changed for jobs that should not be immediately deleted after completion by changing the selection criteria to check the status of the job, for example, sp_help_job, passing @job_name or @job_id and @execution_status = 0.
Maashu
source share