對我來說,解決方案最終不是使用內置的「執行SQL Server代理作業任務」,而是使用「執行T-SQL語句任務」並調用存儲過程,直到完成阻塞...
甜成功:-)
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
而且......
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