I have my stored procedure with an input parameter and an output parameter. I created parameters and was able to execute the job for the first time and received the output value in vJobstatus
variable. When I triggered the stored procedure for a second time, I am getting NULL in cmd.Parameters("@vJobStatus").Value
.
Please help me to get the value of output parameter. TestJob1 is triggered, but the output value is NULL.
Public pm As ADODB.Parameter
Public cnnStr, StrQuery, vJobName As String
Public RunStatus, vJobStatus As Integer
Global Flag_Chkbox As Integer
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "Run_SQL_Job"
cmd.CommandTimeout = 120
Set pm = New ADODB.Parameter
Set pm = cmd.CreateParameter("@vJobName", adVarChar, adParamInput, 50, "TestJob")
cmd.Parameters.Append pm
Set pm = cmd.CreateParameter("@vJobStatus", adInteger, adParamOutput, , vJobStatus)
cmd.Parameters.Append pm
cmd.Execute
vJobStatus = cmd.Parameters("@vJobStatus").Value
If vJobStatus <> 1 Then
cnn.Close
Application.Interactive = True
Application.StatusBar = ""
Exit Sub
Else
cmd.Parameters("@vJobName").Value = "TestJob1"
cmd.Execute
vJobStatus = cmd.Parameters("@vJobStatus").Value
CREATE PROCEDURE [dbo].[Run_SQL_Job]
@vJobName VARCHAR(50)
,@vJobStatus INTEGER OUTPUT
AS
DECLARE @vRunStatus as INT
DeclarE @vJobStats as INT
WAITFOR DELAY '00:00:5';
SET @vRunStatus = (SELECT Runstatus from [dbo].[VIEW])
WHILE @vRunStatus >= 1
BEGIN
SET @vRunStatus = (SELECT Runstatus from [dbo].[VIEW])
END
IF @vRunStatus = 0
BEGIN
EXEC msdb.dbo.sp_start_job @vJobname
WAITFOR DELAY '00:00:10';
END
SET @vRunStatus = (SELECT Runstatus from [dbo].[VIEW])
WHILE @vRunStatus >= 1
BEGIN
SET @vRunStatus = (SELECT Runstatus from [dbo].[VIEW])
END
SELECT @vJobStatus = run_status FROM msdb.dbo.sysjobhistory
where instance_id = (SELECT max(instance_id) from msdb.dbo.sysjobhistory where step_name = @vJobName)
GO