从ADO命令对象调用时,必须声明Oracle ORA-06550标识符

时间:2013-08-28 15:46:22

标签: oracle vba stored-procedures ado adodb

我正在尝试使用Access / VBA从我的Oracle存储过程中使用ADO Command对象检索结果集。

我一直收到错误:

[Oracle][ODBC][Ora]ORA-06550: line 1, column 19:
PLS-00201: identifier 'RETURNDATA' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

我已经在网上阅读了很多关于它是如何完成的文章 - 并尝试了不同的方法(即使用execute而不是命令对象),但我似乎无法让它工作。

微软有一篇关于如何执行此操作的知识库文章很好 - 但尽管遵循了他们的例子 - 我似乎无法让它发挥作用。 http://support.microsoft.com/kb/176086

当我在Oracle SQL Developer中运行PL / SQL 时,返回值列在输出变量窗口中。这告诉我存储过程都正常,并且已经授予必要的权限。

Oracle版本:

SELECT * FROM v $ version

Oracle9i Enterprise Edition Release 9.2.0.8.0 - Production
PL/SQL Release 9.2.0.8.0 - Production
"CORE   9.2.0.8.0   Production"
TNS for 32-bit Windows: Version 9.2.0.8.0 - Production
NLSRTL Version 9.2.0.8.0 - Production

Oracle软件包定义:

CREATE OR REPLACE
PACKAGE test AUTHID DEFINER AS
  PROCEDURE testSP (returnData OUT NUMBER);
END test;
/
CREATE OR REPLACE
PACKAGE BODY test AS
  PROCEDURE testSP (returnData OUT NUMBER) IS
    BEGIN
      returnData := 7;
    END;
END test;

MS Access VBA代码

Public Sub testSP()
  Dim oraConn As ADODB.Connection
  Dim oraCmd As ADODB.Command
  Dim oraRetSet As ADODB.Recordset
  Dim oraNum As Integer

  Set oraConn = New ADODB.Connection

  ' Discovered I need to set this to Read/Write or the SP cannot do any INSERT operations
  oraConn.Mode = adModeReadWrite

  ' Connect to the Oracle Server
  oraConn.Open "Driver={Oracle in OraHome102};dbq=oradsn;Uid=username;Pwd=password"

  ' Set up our Command.
  Set oraCmd = New ADODB.Command
  Set oraCmd.ActiveConnection = oraConn
  oraCmd.CommandType = adCmdText
  oraCmd.Parameters.Append oraCmd.CreateParameter("returnData", adNumeric, adParamOutput)
  oraCmd.CommandText = "{CALL test.testSP({resultset 1, returnData})}"

  ' Set up our Recordset
  Set oraRetSet = New ADODB.Recordset
  oraRetSet.CursorType = adOpenStatic

  ' Attach our Recordset to our Command
  Set oraRetSet.Source = oraCmd

  ' Open our Recordset
  oraRetSet.Open

  ' Retreive our Stored Procedure Result
  oraNum = oraRetSet.Fields(0).Value

  ' Display Result to user
  Debug.Print ">>> Return Value: " & oraNum

  ' Close the Recordset & Connection Objects
  oraRetSet.Close
  oraConn.Close

  ' Clean Up
  Set oraConn = Nothing
  Set oraCmd = Nothing
  Set oraRetSet = Nothing

End Sub

我真的在画空白 - 这意味着它可能是我错过的一小部分。任何帮助将不胜感激。

8月29日更新 我没有取得任何进展。我尝试从ODBC切换到OLEDB提供程序,继另一个stackoverflow问题/答案作为指南:

How do I call an Oracle stored procedure from an Excel VBA script?

' Connect to the Oracle Server
oraConn.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=oradsn;" & _
                 "User ID=username;Password=password;"
oraConn.Open

Set oraCmd = New ADODB.Command
Set oraCmd.ActiveConnection = oraConn
oraCmd.Parameters.Append oraCmd.CreateParameter("returnData", adSmallInt, adParamOutput)
oraCmd.Properties("PLSQLRSet") = True
oraCmd.CommandText = "{CALL test.testSP(?)}"
Set oraRetSet = oraCmd.Execute
oraCmd.Properties("PLSQLRSet") = False

oraNum = oraRetSet.Fields(0).Value

这消除了未声明的标识符,但它不返回任何记录集.. oraRetSet.Fields.Count等于0

1 个答案:

答案 0 :(得分:1)

终于弄清楚发生了什么。

这个网站终于开启了亮点: http://www.oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php

我试图返回一个特定的值 - 一个错误代码。在ADO中使用记录集更专门用于返回数据集。

如果要返回光标ref,作为记录集,需要将OUT参数设置为游标参考数据类型,例如SYS_REFCURSOR。

create or replace
PROCEDURE list_staff (r_stafflist OUT SYS_REFCURSOR) AS 
    BEGIN 
      OPEN r_stafflist FOR
        SELECT name,
               telephone,
               department
        FROM   stafflist
        ORDER BY name;
    END list_staff;

然后,当发出命令时,ADO将自动绑定到引用游标。您不要将光标添加到ADO Command对象参数。