Returning a Table over OCI8 from Oracle Stored Procedure
-
06-03-2021 - |
Pergunta
I am connecting to Oracle via OCI8.
I have a stored procedure:
PROCEDURE ocigetaccounts(accounts OUT SYS_REFCURSOR)
IS BEGIN
OPEN accounts FOR
SELECT * FROM tbaccounts;
END ocigetaccounts;
And I am attempting to return it to PHP using OCI:
$sqlString = 'BEGIN accounts.ocigetaccounts(:accounts); END;';
oci_bind_by_name($statement, ':accounts', $result, -1);
echo $result;
(The rest of the required php-side OCI is in place.)
The error I am getting is:
Warning: oci_execute(): ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'OCIGETACCOUNTS'
How can I get $result to container the table resource
Solução
As far as I can tell, $result
will contain the resource you are after.
The error you are getting is because the cursor is not defined as a type cursor.
You have to explicitly define $result
as a cursor
$result = oci_new_cursor( $dbci );
If $result
is returned, it will be returned as a resource and you should handle it just as you would handle a any other returned resource.
For your example (with $dbci
being your connection resource):
$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.
}
This is how we handle cursors returned from the database. Hope this solves the problem
Outras dicas
There are issues with PHP and REFCURSORS. See this excellent blog article for a complete explanation, and workaround:
http://blogs.oracle.com/opal/entry/converting_ref_cursor_to_pipe
Hope that helps.