15.31 OPEN_REMOTE_SQL_QUERYファンクション

このファンクションは、問合せコンテキストを開き、ORDS REST対応SQLインスタンスに対して指定されたSQL問合せを実行します。

構文

function open_remote_sql_query(
    p_server_static_id      in varchar2,
    p_sql_query             in varchar2,
    p_sql_parameters        in t_parameters default c_empty_parameters,
    p_auto_bind_items       in boolean      default true,
    --
    p_first_row             in pls_integer  default null,
    p_max_rows              in pls_integer  default null,
    --
    p_total_row_count       in boolean      default false,
    p_total_row_count_limit in pls_integer  default null )
    return t_context;

パラメータ

表15-29 OPEN_REMOTE_SQL_QUERYファンクションのパラメータ

パラメータ 説明

p_server_static_id

ORDS REST対応SQLインスタンスの静的IDです。

p_sql_query

実行するSQL問合せ。

p_sql_parameters

リモート・サーバーに渡すバインド変数。

p_auto_bind_items

すべてのページ・アイテムを自動的にバインドするかどうか。

p_first_row

結果セットからフェッチする最初の行。

p_max_rows

フェッチする行の最大数。

p_total_row_count

総行数を求めるかどうか。

p_total_row_count_limit

総行数計算の上限値。

戻り値

Webソース問合せのカーソルを表すコンテキスト・オブジェクト。

次の例では、REST対応ORDSインスタンスが静的ID My_Remote_SQL_Instanceで共有コンポーネントに構成されていることを前提としています。それに基づいて、リモート・サーバーに問合せを実行し、結果セットを出力します。このサンプル・コードは、プラグイン内や「PL/SQL実行」リージョン内で使用できます。

declare
    l_context apex_exec.t_context;

    l_idx_empno    pls_integer;
    l_idx_ename    pls_integer;
    l_idx_job      pls_integer;
    l_idx_hiredate pls_integer;
    l_idx_mgr      pls_integer;
    l_idx_sal      pls_integer;
    l_idx_comm     pls_integer;
    l_idx_deptno   pls_integer;

begin
    l_context := apex_exec.open_remote_sql_query(
         p_server_static_id   => 'My_Remote_SQL_Instance',
         p_sql_query          => 'select * from emp' );

    l_idx_empno    := apex_exec.get_column_position( l_context, 'EMPNO'); 
    l_idx_ename    := apex_exec.get_column_position( l_context, 'ENAME'); 
    l_idx_job      := apex_exec.get_column_position( l_context, 'JOB'); 
    l_idx_hiredate := apex_exec.get_column_position( l_context, 'HIREDATE'); 
    l_idx_mgr      := apex_exec.get_column_position( l_context, 'MGR'); 
    l_idx_sal      := apex_exec.get_column_position( l_context, 'SAL'); 
    l_idx_comm     := apex_exec.get_column_position( l_context, 'COMM'); 
    l_idx_deptno   := apex_exec.get_column_position( l_context, 'DEPTNO'); 

    while apex_exec.next_row( l_context ) loop

          htp.p( 'EMPNO: ' || apex_exec.get_number  ( l_context, l_idx_empno    ) ); 
          htp.p( 'ENAME: ' || apex_exec.get_varchar2( l_context, l_idx_ename    ) );
          htp.p( 'MGR:   ' || apex_exec.get_number  ( l_context, l_idx_mgr      ) );

    end loop;

    apex_exec.close( l_context );
    return;
exception
    when others then
         apex_debug.log_exception;
         apex_exec.close( l_context );
    raise;    
end;