通过OCI8从Oracle存储过程返回一个表


Returning a Table over OCI8 from Oracle Stored Procedure

我正在通过OCI8连接Oracle。

我有一个存储过程:
  PROCEDURE ocigetaccounts(accounts OUT SYS_REFCURSOR)
  IS BEGIN
  OPEN accounts FOR
        SELECT * FROM tbaccounts;
  END ocigetaccounts;

和我试图返回它到PHP使用OCI:

$sqlString = 'BEGIN accounts.ocigetaccounts(:accounts); END;';
oci_bind_by_name($statement, ':accounts', $result, -1);
    echo $result;

(其余所需的php端OCI已就位)

我得到的错误是:

警告:oci_execute(): ORA-06550:第1行,第7列:PLS-00306:调用'OCIGETACCOUNTS'时参数的数量或类型错误

如何获得$result容器表资源

据我所知,$result将包含您所需要的资源。您得到的错误是因为游标没有定义为类型游标。必须显式地将$result定义为游标

$result = oci_new_cursor( $dbci );

如果返回$result,它将作为一个资源返回,您应该像处理任何其他返回的资源一样处理它。

对于您的示例(使用$dbci作为连接资源):

$sqlString = 'BEGIN accounts.ocigetaccounts(:accounts); END;';
$stmt = oci_parse ( $dbci, $sqlString );
//Declare cursor
$result = oci_new_cursor( $dbci );
//Bind cursor
oci_bind_by_name ( $stmt, ':accounts', $result, -1, OCI_B_CURSOR);
//Execute query
if (oci_execute ( $stmt )) {
    //Execute cursor
    oci_execute($result);  //Or you can return the cursor.
}

这是我们如何处理从数据库返回的游标。希望这能解决问题

PHP和REFCURSORS存在问题。有关完整的解释和解决方法,请参阅这篇优秀的博客文章:

http://blogs.oracle.com/opal/entry/converting_ref_cursor_to_pipe

希望对你有帮助。