2

我正在尝试使用VBA将记录集检索回ADODB对象。我在 Oracle(11g) 数据库包中有一个非常基本的功能:

            CREATE OR REPLACE PACKAGE "DATABASE"."PKG_INDEX" AS
            FUNCTION get_diary(event_date IN DATE := NULL) RETURN SYS_REFCURSOR;
            END PKG_AIT_INDEX;

            CREATE OR REPLACE PACKAGE BODY "DATABASE"."PKG_INDEX" AS
            FUNCTION get_diary(event_date IN DATE := NULL)
            RETURN SYS_REFCURSOR
            IS
            my_cursor  SYS_REFCURSOR;
            search_date EVENTS.EVENT_DATE%TYPE;
            BEGIN
            If event_date Is Null Then
            search_date := TRUNC(SYSDATE);
            Else
            search_date := event_date;
            END IF;
            OPEN my_cursor FOR
            SELECT *
            FROM Table
            WHERE EVENT_DATE > search_date;
            RETURN my_cursor;
            END get_diary;
            END PKG_INDEX;

在 DB Visualiser 中调用时,该函数的行为与预期相同:

            --/
            DECLARE
            rc SYS_REFCURSOR;
            BEGIN
            rc := PKG_INDEX.GET_DIARY(NULL);
            END;
            /

我想从 VBA 调用函数并获取记录集。我尝试了许多不同的方法,但无法提出解决方案。

            Public Function ExecuteFunction(sDBName As String, sDBEnv As String, sDBRegion As String, sCommand As String, Optional sDBConn As String, Optional sError As String) As Recordset
            Dim oConn As ADODB.Connection
            Dim oCmd As ADODB.Command
            Dim oParam As ADODB.Parameter
            Dim oRst As ADODB.Recordset

            Const SOURCE As String = "ExecuteQuery"

            If GetDBConnection(oConn, sDBName, sDBEnv, sDBRegion, sDBConn, sError) Then

            Set oCmd = New ADODB.Command
            With oCmd
            Set .ActiveConnection = oConn
            .CommandType = adCmdText
            .CommandText = "BEGIN :returnValue :=PKG_INDEX.GET_DIARY; END;"

            Set oParam = .CreateParameter("returnValue", adVariant, adParamInputOutput, 100000)
            .Parameters.Append oParam

            Set oRst = .Execute
            End With
            Set ExecuteFunction = oRst

            End If

            End Function

以上是一次尝试,但没有任何内容传递回参数,我得到:

ORA-01008: not all variables bound

我的连接字符串是:

Provider=MSDAORA;User ID=XXX;Password=XXX;Data Source=(DESCRIPTION = (ADDRESS_LIST 
= (ADDRESS = (PROTOCOL = TCP)(HOST = XXXX)(PORT = XXXX)) ) (CONNECT_DATA = (SID = XXXX)));

似乎很难实现很容易的事情。

任何帮助或建议将不胜感激。

终于找到了一个很好的例子:

从 ADO 调用存储过程

或者使用 CreatePLSQLDynaset 方法:

CreatePLSQLDynaset 方法

4

0 回答 0