使用SQL Agent时的进程受限

时间:2015-01-06 01:29:01

标签: sql-server-2008 vbscript sql-agent-job

我有一个VBScript文件,它调用另一个脚本进行并行执行。

第一个VBScript的名称是Root.vbs

Option Explicit
On Error Resume Next

Sub ParallelExecute(idx,strlog)
    Dim objShell, strCmd
    Set objShell = Wscript.CreateObject("Wscript.Shell")
    strCmd = "%comspec% /c cscript  C:\TestVbs\test.vbs " & idx & " " & strlog
    objShell.Run strCmd
    objShell = Nothing
End Sub

Dim intIdx
Dim strLog, strLog1, strLog2
strLog = "C:\TestVbs\Log\Root"
'parallel executing
For intIdx = 0 to 150
    strLog1 = strLog & intIdx & ".txt"
    Call ParallelExecute(intIdx, strLog1)
Next
Wscript.Quit

Sub WriteLog(strLogPath, idx)   
    Dim objFile, objFso, strContent
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFso.OpenTextFile(strLogPath, 8, True)
    strContent = idx
    Dim zzz
    For zzz=0 to 2
        objFile.WriteLine(FormatDateTime(Now) & "    ROOT    " & strContent)
    Next
    objFile.Close
End Sub

第二个VBScript的名称是test.vbs

Option Explicit
On Error Resume Next

Dim idx, time, strLogPath
idx = WScript.Arguments(0)
strLogPath = "C:\TestVbs\Log\" & idx & ".txt"
Call WriteLog(strLogPath,idx)
Wscript.Quit

Sub WriteLog(strLogPath, idx)   
    Dim objFile, objFso, strContent
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFso.OpenTextFile(strLogPath, 8, True)
    strContent = idx
    Dim intIdx
    For intIdx=0 to 10
        objFile.WriteLine(FormatDateTime(Now) & "    SUB     " & strContent)
        Wscript.Sleep 6000
    Next
    objFile.Close
End Sub

我有一个SQL代理脚本:

USE msdb
GO
--
BEGIN TRANSACTION
DECLARE @filename nvarchar(100)
DECLARE @csCommand nvarchar(255)
DECLARE @jobId BINARY(16)
DECLARE @ReturnCode INT

SET @filename = 'C:\TestVbs\Log\log.txt'
SET @ReturnCode = 0
SET @csCommand = 'cscript "C:\TestVbs\Root.vbs"' 

-- Create Job
EXEC @ReturnCode = sp_add_job 
    @job_name = N'JOBTEST' -- Job name 
    , @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- Create Job Step1
EXEC @ReturnCode = sp_add_jobstep
@job_name = N'JOBTEST' -- Must be the same as the name of job has just created above 
, @step_name = N'JOBTEST' -- Step name
, @on_success_action= 1 
, @subsystem=N'CmdExec' -- Specify that this step calls a cmd command
, @command=@csCommand -- Cmd command
, @output_file_name= @filename  -- Specify log file name
, @flags=2 -- Appending log to existing file
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

EXEC @ReturnCode = sp_add_jobschedule
@job_id=@jobId -- Specify the jobID that this scheduler will plan for 
, @name=N'JOBTEST_SCH' -- Schedule name
, @freq_type=4  
, @freq_interval=1
, @active_start_time=030000
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

-- Add job to Server
EXEC @ReturnCode = sp_add_jobserver
@job_name = N'JOBTEST' -- Add to jobserver
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

当我使用命令行执行Root.vbs脚本时,所有进程(根进程和子进程)都被创建并正常运行,但是当我使用SQL代理执行Root.vbs脚本时,某些进程(子流程)未创建。

我认为子命令行受SQL Server的限制,但我不知道如何配置SQL Agent。

0 个答案:

没有答案