可能重複:
Executing SQL Server Agent Job from a stored procedure and returning job resultsp_start_job等待作業完成
有沒有辦法來確定何時因爲一旦它已經開始與sp_start_job完成了一個SQL代理工作?
可能重複:
Executing SQL Server Agent Job from a stored procedure and returning job resultsp_start_job等待作業完成
有沒有辦法來確定何時因爲一旦它已經開始與sp_start_job完成了一個SQL代理工作?
XP_SQLAGENT_ENUM_JOBS
可以使用,但它沒有證件。 它通常用於檢測長時間運行的作業。
當然,也有sp_help_jobs
或者乾脆監控作業歷史表
實際上我最近做了,這是我正在考慮實現它。我通過sp_add_job和sp_add_jobstep創建臨時作業,並將@delete_level設置爲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
sp_help_job @job_name @execution_status = 0
這article描述了SP推出SQL代理作業,等待。
-- 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 [email protected]_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
那篇文章幫了我,反正... – topwik 2013-03-27 17:43:15
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
)
我寧願留在記錄的土地。我知道sp_help_jobs,但它有點難看,因爲根據我的理解,我需要將結果集讀入臨時表中,然後從中選擇。我正在尋找一些更清潔的東西,雖然這會起作用。 – 2008-12-16 16:42:16