将SYS_REFCURSOR从Oracle函数/存储过程返回到VBA

时间:2013-08-27 20:00:47

标签: oracle vba excel-vba oracle11g adodb

我正在尝试使用 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)));

似乎很难实现非常简单的事情。

非常感谢任何帮助或建议。

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

Calling Stored Procedures From ADO

或使用CreatePLSQLDynaset方法:

CreatePLSQLDynaset Method

0 个答案:

没有答案