通过OraOLEDB运行存储过程

时间:2018-07-03 18:26:47

标签: excel-vba stored-procedures oraoledb vba excel

我有以下存储过程,必须从VBA调用。

declare
    p_segment1 varchar(25);
    p_segment2 varchar(25);
    p_segment3 varchar(25);
    p_segment4 varchar(25);
    p_segment5 varchar(25);
    p_valid_flag varchar(1);
begin
    p_segment1 := 'code segment here';
    p_segment2 := 'code segment here';
    p_segment3 := 'code segment here';
    p_segment4 := 'code segment here';
    p_segment5 := 'code segment here';

    apps.key_validate_gl_code_comb(p_segment1, p_segment2, p_segment3, p_segment4, p_segment5, p_valid_flag);
    dbms_output.put_line('Flag: ' || p_valid_flag);
end;

我需要从Excel在VBA中调用此过程,但是通过运行以下代码却收到错误消息:

    Set pCommand = New Command
    Set pProcedureRecordset = New Recordset
    With pCommand
        Set .ActiveConnection = pConnection
        .CommandType = adCmdStoredProc
        Set p_Segment1 = .CreateParameter("p_segment1", adVarChar, adParamInput, 25, 'code segment here')
        Set p_Segment2 = .CreateParameter("p_segment2", adVarChar, adParamInput, 25, 'code segment here')
        Set p_Segment3 = .CreateParameter("p_segment3", adVarChar, adParamInput, 25, 'code segment here')
        Set p_Segment4 = .CreateParameter("p_segment4", adVarChar, adParamInput, 25, 'code segment here')
        Set p_Segment5 = .CreateParameter("p_segment5", adVarChar, adParamInput, 25, 'code segment here')
        Set p_Valid_Flag = .CreateParameter("p_valid_flag", adVarChar, adParamInputOutput, 1)
        .Parameters.Append p_Segment1
        .Parameters.Append p_Segment2
        .Parameters.Append p_Segment3
        .Parameters.Append p_Segment4
        .Parameters.Append p_Segment5
        .Parameters.Append p_Valid_Flag
        .CommandText = "apps.key_validate_gl_code_comb(?,?,?,?,?,?)"
        Set pProcedureRecordset = .Execute
    End With

当我在SQL Developer中运行此命令时,会得到所需的输出,但是当我从VBA运行它时,会显示一条Unspecified Error消息。

如何从VBA调用存储过程以获取输出?

1 个答案:

答案 0 :(得分:0)

假设您的连接正确,并且您只想获取输出var作为结果,则不需要任何Recordset变量(如果execute返回一个记录集,它将为您创建它)。

我发现一些要纠正的地方,之后我可以成功执行您的代码

  • 即使p_valid_flag,也必须设置一个值。
  • 在没有?占位符的情况下调用proc。

只是一个建议(与您的实际问题无关):声明变量并使用Option Explicit

 Set pCommand = New ADODB.Command
 With pCommand

    Set .ActiveConnection = pConnection
    .CommandType = adCmdStoredProc

    Set p_Segment1 = .CreateParameter("p_segment1", adVarChar, adParamInput, 25, "code segment here")
    ...
    Set p_Valid_Flag = .CreateParameter("p_valid_flag", adVarChar, adParamInputOutput, 1, "")

    .Parameters.Append p_Segment1
    ...
    .Parameters.Append p_Valid_Flag

    .CommandText = "apps.key_validate_gl_code_comb"
    .Execute
    Debug.Print "-->" & p_Valid_Flag.Value
End With