OCILIB Tutorials v5.0.0
Intermediatedemo/plsql_table.cplsql_table.sql

PL/SQL index-by tables — array parameters

Objectives — by the end of this page you will be able to:

Setup (from demo/plsql_table.sql)

create or replace package test_plsqltables as
    type t_tab_names  is table of varchar2(50) index by binary_integer;
    type t_tab_values is table of varchar2(50) index by binary_integer;
    procedure combine_values
        (p_names in t_tab_names, p_values in t_tab_values,
         p_results out t_tab_values);
end;
/

Binding table parameters

A TABLE OF varchar2 INDEX BY … parameter maps to an array of buffers bound with the array-bind family — element capacity + count go in the bind call, and the direction flag marks which tables come back:

#define SIZE_NAME  50
#define SIZE_VALUE 50
#define COUNT      4

otext tab_names [COUNT][SIZE_NAME];
otext tab_values[COUNT][SIZE_VALUE];
otext tab_out   [COUNT][SIZE_VALUE];

OCI_StatementPrepare(st, OTEXT("begin test_plsqltables.combine_values"
                      "(:tab_names, :tab_values, :tab_out); end;"));

OCI_StatementBindStringArray(st, OTEXT(":tab_names"),  (otext*)tab_names,  SIZE_NAME,  COUNT);
OCI_StatementBindStringArray(st, OTEXT(":tab_values"), (otext*)tab_values, SIZE_VALUE, COUNT);
OCI_StatementBindStringArray(st, OTEXT(":tab_out"),    (otext*)tab_out,    SIZE_VALUE, COUNT);

/* directions: first two IN, third OUT */
OCI_BindSetDirection(OCI_StatementGetBind(st, 0), OCI_BDM_IN);
OCI_BindSetDirection(OCI_StatementGetBind(st, 1), OCI_BDM_IN);
OCI_BindSetDirection(OCI_StatementGetBind(st, 2), OCI_BDM_OUT);

OCI_StatementExecutePrepared(st);        /* tab_out[][] now holds the combined strings */

Numeric tables bind plain C/vector arrays — no element-size arg needed:

OCI_Int32 ids[64];
OCI_StatementBindInt32Array(st, OTEXT(":ids"), ids, 64);

Rules that differ from array DML binds

Putting it together — the typical sequence

PL/SQL tables (associative arrays) are the collection type that only exists in PL/SQL:

  1. Declare the type in a package: type t is table of number index by binary_integer.
  2. Bind an array — StatementBind*Array binds a C array into the PL/SQL table slot, element for element.
  3. Direction matters — IN tables send your array in, OUT tables come back filled, IN OUT do both.

Hints: the array size must match the table's capacity — Oracle won't resize a PL/SQL table for you; index by binary_integer tables are the common case — index by varchar2 tables are rarer and bound the same way; and an OUT table bind's array comes back fully populated — initialize it even though it's "output".

Try it yourself

  1. Run plsql_table.sql + the sample; print tab_out.
  2. Pass an in out table and modify two elements server-side.
  3. Bind a table of number parameter with OCI_StatementBindInt32Array.

Common pitfalls

Recap

Next: DBMS_OUTPUT — printing server-side text from PL/SQL.