代码之家  ›  专栏  ›  技术社区  ›  Greg Dean

sp_开始_作业等待作业完成[重复]

  •  6
  • Greg Dean  · 技术社区  · 17 年前

    可能重复:
    Executing SQL Server Agent Job from a stored procedure and returning job result

    使用sp_start_作业启动sql代理作业后,是否有方法确定它何时完成?

    5 回复  |  直到 9 年前
        1
  •  4
  •   topwik    13 年前

    这 article 描述启动sql代理作业并等待的SP。

    -- output from stored procedure xp_sqlagent_enum_jobs is captured in the following table
        declare @xp_results TABLE ( job_id                UNIQUEIDENTIFIER NOT NULL,
                                    last_run_date         INT              NOT NULL,
                                    last_run_time         INT              NOT NULL,
                                    next_run_date         INT              NOT NULL,
                                    next_run_time         INT              NOT NULL,
                                    next_run_schedule_id  INT              NOT NULL,
                                    requested_to_run      INT              NOT NULL, -- BOOL
                                    request_source        INT              NOT NULL,
                                    request_source_id     sysname          COLLATE database_default NULL,
                                    running               INT              NOT NULL, -- BOOL
                                    current_step          INT              NOT NULL,
                                    current_retry_attempt INT              NOT NULL,
                                    job_state             INT              NOT NULL)
    
        -- start the job
        declare @r as int
        exec @r = msdb..sp_start_job @job
    
        -- quit if unable to start
        if @r<>0
            RAISERROR (N'Could not start job: %s.', 16, 2, @job)
    
        -- start with an initial delay to allow the job to appear in the job list (maybe I am missing something ?)
        WAITFOR DELAY '0:0:01';
        set @seccount = 1
    
        -- check job run state
        insert into @xp_results
        execute master.dbo.xp_sqlagent_enum_jobs 1, @job_owner, @job_id
    
        set @running= (SELECT top 1 running from @xp_results)
    
        while @running<>0
        begin
            WAITFOR DELAY '0:0:01';
            set @seccount = @seccount + 1
    
            delete from @xp_results
    
            insert into @xp_results
            execute master.dbo.xp_sqlagent_enum_jobs 1, @job_owner, @job_id
    
            set @running= (SELECT top 1 running from @xp_results)
        end
    
        -- result: not ok (=1) if still running
    
        if @running <> 0 begin
            -- still running
            return 0
        end
        else begin
    
            -- did it finish ok ?
            set @run_status = 0
    
            select @run_status=run_status
            from msdb.dbo.sysjobhistory
            where job_id=@job_id
              and cast(run_date as bigint) * 1000000 + run_time >= @start_job
    
            if @run_status=1
                return 1  --finished ok
            else  --error
                RAISERROR (N'job %s did not finish successfully.', 16, 2, @job)
    
        end
    
        END TRY
    
        2
  •  3
  •   gbn    17 年前

    XP_SQLAGENT_ENUM_JOBS 可以使用,但未记录。 它通常用于检测长时间运行的作业。

    当然也有 sp_help_jobs 或者只是监视工作历史记录表

        3
  •  1
  •   Taryn Frank Pearson    14 年前
     sp_help_job   @job_name   @execution_status = 0
    
        4
  •  1
  •   Kevin Fairchild    14 年前
    SELECT TOP 1 1 AS FinishedRunning
    FROM msdb..sysjobactivity aj
    JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
    WHERE aj.stop_execution_date IS NOT NULL
    AND aj.start_execution_date IS NOT NULL
    AND sj.name = 'YourJobNameHere'
    AND NOT EXISTS
    (
        SELECT TOP 1 1
        FROM msdb..sysjobactivity New
        WHERE New.job_id = aj.job_id
        AND new.start_execution_date > aj.start_execution_date
    )
    
        5
  •  1
  •   GEOCHET S.Lott    14 年前

    事实上,我最近不得不这么做,这就是我考虑实施它的方式。我正在通过sp_add_job和sp_add_jobstep创建一个临时作业,并将@delete_级别设置为3(运行后始终删除)。

    我并不是100%赞同这种方法,从存储过程的标题可以看出这一点。然而,它确实有效:

    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
    

    当然,你会想输入一些代码,以确保有一个最大的次数,这将递归,但你明白的想法。您还可以传入一个参数来控制递归发生的频率。就我而言,十秒钟后,结果毫无意义。

    我在这里所做的工作可以通过更改选择条件来检查作业的执行状态,例如,通过sp_help_job传递@job_name或@job_id和@execution_status=0来修改,以便在执行后不会立即删除作业。

    推荐文章