0

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
9
  • How do you know it's not coming from the stored procedure? Commented Oct 10, 2021 at 22:36
  • There are at leat four different ways to return values from stored procedures. Please post your stored procedure code so we know how you're doing it. Commented Oct 10, 2021 at 22:46
  • @Nick.McDermaid, I have posted the stored procedure. Commented Oct 10, 2021 at 22:53
  • @GSerg, when I used Debug.print cmd.Parameters("@vJobStatus").Value, it is NULL Commented Oct 10, 2021 at 22:54
  • 1
    Add set @vJobStatus = isnull(@vJobStatus, 42) to the end of your procedure and see if you get 42 in VBA. Commented Oct 10, 2021 at 23:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.