RefCursorSupport¶
AdvancedServerOpenClientLibraryは、Oracleと互換性のあるPL/SQLプロシージャの OUT パラメータとして REF CURSOR の使用をサポートしています。次のAPIを通じてサポートが提供されます。
OCIBindByNameOCIBindByPosOCIBindDynamicOCIStmtPrepareOCIStmtExecuteOCIStmtFetchOCIAttrGet
OCLコネクタは SQLT_RSET データ型もサポートしています。
The following example demonstrates how to invoke a stored procedure that
opens a cursor and returns a REF CURSOR as an output parameter. The code
sample assumes that a PL/SQL procedure named openCursor (with an OUT
parameter of type REF CURSOR) has been created on the database server,
and that the required handles have been allocated:
char * openCursor =
"begin \
openCursor(:cmdRefCursor); \
end;";
OCIStmt *stmtOpenRefCursor;
OCIStmt *stmtUseRefCursor;
REF CURSOR を開いて使用するストアドプロシージャを実行するためのハンドルを割り当てます:
/* Handle for the stored procedure to open the ref cursor */
OCIHandleAlloc((dvoid *) envhp,
(dvoid **) &stmtOpenRefCursor,
OCI_HTYPE_STMT,
0,
(dvoid **) NULL));
/* Handle for using the Ref Cursor */
OCIHandleAlloc((dvoid *) envhp,
(dvoid **) &stmtUseRefCursor,
OCI_HTYPE_STMT,
0,
(dvoid **) NULL));
次に、 REF CURSOR を開くために使用するPL/SQLブロックを準備します:
OCIStmtPrepare(stmtOpenRefCursor,
errhp,
(text *) openCursor,
(ub4) strlen(openCursor),
OCI_NTV_SYNTAX,
OCI_DEFAULT));
PL/SQL openCursor OUT パラメータをバインドします:
OCIBindByPos(stmtOpenRefCursor,
&bndplrc1,
errhp,
1,
(dvoid*) &stmtUseRefCursor,
/* the returned ref cursor */
0,
SQLT_RSET,
/* SQLT_RSET type representing cursor */
(dvoid *) 0,
(ub2 *) 0,
(ub2) 0,
(ub4) 0,
(ub4 *) 0,
OCI_DEFAULT));
stmtOpenRefCursor ステートメントハンドルを使用して openCursor プロシージャを呼び出します:
OCIStmtExecute(svchp,
stmtOpenRefCursor,
errhp,
1,
0,
0,
0,
OCI_DEFAULT);
この時点で、 stmtUseRefCursor ステートメントハンドルにはカーソルへの参照が含まれています。情報を取得するには、参照カーソルの出力変数を定義します。
/* Define the output variables for the ref cursor */
OCIDefineByPos(stmtUseRefCursor,
&defnEmpNo,
errhp,
(ub4) 1,
(dvoid *) &empNo,
(sb4) sizeof(empNo),
SQLT_INT,
(dvoid *) 0,
(ub2 *)0,
(ub2 *)0,
(ub4) OCI_DEFAULT));
次に、結果セットの最初の行をターゲット変数にフェッチします。
/* Fetch the cursor data */
OCIStmtFetch(stmtUseRefCursor,
errhp,
(ub4) 1,
(ub4) OCI_FETCH_NEXT,
(ub4) OCI_DEFAULT))