0
我正在處理將使用SSIS中的Attunity連接器將數據從ORACLE加載到SQL Server的進程。我需要加載大約50個表格,併爲每個表格創建了一個作業,以便可以並行加載它們。基於服務器資源,我一次可以運行10個。我有一個腳本,我一直在工作,但它不能正常工作,因爲它激發了10個工作並退出。我需要通過50個作業腳本循環(開始PS_),並只運行10爲並行執行循環作業的SQL腳本
DECLARE @a int
SET @a=0
DECLARE @jobname nvarchar(200)
-- checks if there are already 10 jobs running
WHILE ((@a <10) and (
\t \t \t (SELECT count(*) FROM msdb.dbo.sysjobs a
\t \t \t INNER JOIN msdb.dbo.sysjobactivity b
\t \t \t \t ON a.job_id = B.job_id
\t \t \t WHERE Start_Execution_date is not null
\t \t \t AND Stop_Execution_Date is null
\t \t \t AND substring (name, 1,3) = 'PS_')<= 10))
BEGIN
\t SET @jobname = NULL
\t --loops through to fetch one non-running job at a time and fetches upto 10 jobs
\t SELECT TOP 1 @jobName = name
\t FROM msdb.dbo.sysjobs X
\t WHERE substring (x.name, 1,3) = 'PS_'
\t AND
\t --checks the job did not already run today and is currently running.
\t name IN (
\t \t \t SELECT name FROM msdb.dbo.sysjobs C
\t \t \t LEFT OUTER JOIN (select max(start_execution_date) Start_Execution_date ,job_id
\t \t \t \t \t \t \t from msdb.dbo.sysjobactivity
\t \t \t \t \t \t \t group by job_id) B
\t \t \t ON B.job_id=C.Job_ID
\t where cast(isnull(Start_Execution_date,'01/01/1900') as date) < cast(getdate()as date))
\t SELECT @jobname
\t IF (@jobname is not null)
\t \t BEGIN
\t \t \t EXEC msdb.dbo.sp_start_job @[email protected]
\t \t \t --select @jobname
\t \t END
\t \t WAITFOR DELAY '00:00:02'
\t \t SET @[email protected]+1
END
按計劃安排工作,並錯開開始時間? – Jeremy
看起來您需要一個外部循環來繼續重複內部循環,直到所有50個作業都運行完畢,並將循環控制更改爲<50。 –