简体   繁体   English

SQL Server维护计划任务和完成

[英]Sql Server Maintenance Plan Tasks & Completion

I have a maintenance plan that looks like this... 我有一个看起来像这样的维护计划...

Client 1 Import Data (Success) -> Process Data (Success) -> Post Process (Completion) -> Next Client
Client 2 Import Data (Success) -> Process Data (Success) -> Post Process (Completion) -> Next Client
Client N ...

Import Data and Process Data are calling jobs and Post Process is an Execute Sql task. 导入数据和流程数据正在调用作业,而后流程是Execute Sql任务。 If Import Data or Process Data Fail, it goes to the next client Import Data... 如果导入数据或过程数据失败,它将转到下一个客户端导入数据...

Both Import Data and Process Data are jobs that contain SSIS packages that are using the built-in SQL logging provider. 导入数据和过程数据都是包含使用内置SQL日志记录提供程序的SSIS包的作业。

My expectation with the configuration as it stands is: 我对当前配置的期望是:

  1. Client 1 Import Data Runs: Failure -> Client 2 Import Data | 客户端1导入数据运行:失败->客户端2导入数据| Success Process Data 成功过程数据
  2. Process Data Runs: Failure -> Client 2 Import Data | 流程数据运行:失败->客户端2导入数据| Success Post Process 成功后流程
  3. Post Process Runs: Completion -> Success or Failure -> Next Client Import Data 后期处理运行:完成->成功或失败->下一个客户端导入数据

This isn't what I'm seeing in my logs though... I see several Client Import Data SSIS log entries, then several Post Process log entries, then back to Client Import Data! 不过,这不是我在日志中看到的内容……我看到几个“客户端导入数据” SSIS日志条目,然后是几个“后期处理”日志条目,然后又回到“客户端导入数据”! Arg!! 啊!

What am I doing wrong? 我究竟做错了什么? I didn't think the "success" piece of Client 1 Import Data would kick off until it... well... succeeded aka finished! 我不认为“客户端1导入数据”的“成功”部分会开始,直到...成功完成。 The logs seem to indicate otherwise though... 日志似乎表明并非如此...

I really need these tasks to be consecutive not concurrent. 我真的需要这些任务是连续的而不是并发的。 Is this possible? 这可能吗?

Thanks! 谢谢!

尝试将序列容器放在需要成组执行的任务周围。

For me the workaround ended up being NOT using the built-in "Execute SQL Server Agent Job Task" and instead using "Execute T-SQL Statement Task" and calling a stored procedure that blocked until completion... 对我来说,解决方法最终不是使用内置的“执行SQL Server代理作业任务”,而是使用“执行T-SQL语句任务”,而是调用直到完成才阻塞的存储过程。

Sweet success :-) 成功的秘诀:-)

CREATE PROCEDURE [dbo].[SQLJob_RunBlocking]
(
    @JobName SYSNAME
)
AS
BEGIN
    -- running a job returns before the job is complete
    -- this procedure will run the job and loop until its status is complete
    SET NOCOUNT ON;

    DECLARE @JobStatus INT;

    -- start job
    EXECUTE msdb.dbo.sp_start_job @job_name = @JobName;

    -- loop until status is complete
    WHILE ISNULL(@JobStatus, 0) != 4 BEGIN
        WAITFOR DELAY '00:00:01';

        EXECUTE dbo.SQLJob_GetStatus @job_name = @JobName, @select_data = 0, @execution_status = @JobStatus OUTPUT;
    END
END

And... 和...

CREATE PROCEDURE [dbo].[SQLJob_GetStatus]
(
    @job_name SYSNAME
    ,@select_data INT = 0
    ,@execution_status INT = NULL OUTPUT
)
AS
BEGIN
    SET NOCOUNT ON;

    -- http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-sql-job-execution-status.html
    /*
        Is the execution status for the jobs. 
        Value Description 
        0 Returns only those jobs that are not idle or suspended.  
        1 Executing. 
        2 Waiting for thread. 
        3 Between retries. 
        4 Idle. 
        5 Suspended. 
        7 Performing completion actions 
    */

    DECLARE @job_id UNIQUEIDENTIFIER 
        ,@is_sysadmin INT
        ,@job_owner SYSNAME;

    SELECT @job_id = job_id FROM msdb.dbo.sysjobs_view where name = @job_name;
    SELECT @is_sysadmin = ISNULL(IS_SRVROLEMEMBER(N'sysadmin'), 0);
    SELECT @job_owner = SUSER_SNAME();

    CREATE TABLE #xp_results (
        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
    );


    IF ((@@microsoftversion / 0x01000000) >= 8) -- SQL Server 8.0 or greater
        INSERT INTO #xp_results
        EXECUTE master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner, @job_id;
    ELSE
        INSERT INTO #xp_results
        EXECUTE master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner;

    --declare @execution_status int
    SET @execution_status = (SELECT job_state FROM #xp_results);

    DROP TABLE #xp_results;

    IF @select_data = 1 
        SELECT @job_name AS 'job_name', @execution_status AS 'execution_status';
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM