The BIND_ARRAY stored procedure is used to bind a PL/SQL collection to the placeholders in a dynamic SQL statement.
Unlike BIND_VARIABLE, which binds a value to only one placeholder at a time, BIND_ARRAY binds a value to an entire set of placeholders. When executing the statement using EXECUTE, OceanBase Database executes the statement in rounds, processing each element of the set one by one, thereby reducing the number of SQL executions.
Starting from V4.4.2 BP4, OceanBase Database supports the BIND_ARRAY stored procedure.
Applicability
This content applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL-compatible mode.
Syntax
DBMS_SQL.BIND_ARRAY(
c IN INTEGER,
name IN VARCHAR2,
table_value IN <table_type>,
index1 IN INTEGER DEFAULT 1,
index2 IN INTEGER DEFAULT -1);
Here, table_value and its corresponding <table_type> can be any of the following pairs. BIND_ARRAY is overloaded to accept different set types:
<table_value> <table_type>
n_tab Number_Table
c_tab Varchar2_Table
d_tab Date_Table
bl_tab Blob_Table
cl_tab Clob_Table
bf_tab Binary_Float_Table
bd_tab Binary_Double_Table
Parameters
Parameters |
Explanation |
|---|---|
| c | The cursor ID of the set to be bound. |
| name | The name of the placeholder in the statement. The name can contain a leading colon (:). |
| table_value | The PL/SQL collection variable to bind. |
| index1 | The starting index for the bind. If not specified,index1andindex2When using add, the entire collection is bound. |
| index2 | End index of the bound range. If not specified,index1andindex2When using addAll, the entire collection is bound. |
Usage instructions
Supported collection types include
Number_Table,Varchar2_Table,Date_Table,Blob_Table,Clob_Table,Binary_Float_Table, andBinary_Double_Table. The collection itself can be an associative array, a nested table, or a VARRAY.Both
index1andindex2must be specified or both omitted. If both are omitted, all elements of the bound set are involved. If both are specified, only the elements with indices in the range[index1, index2]are involved. For associative arrays, elements deleted byDELETEare not involved in the binding.An error
ORA-06532is returned whenindex1orindex2is out of the index range of the collection.A placeholder can only be bound once. When calling
BIND_ARRAYorBIND_VARIABLErepeatedly for the same placeholder, the later binding overwrites the previous one.The same cursor can mix
BIND_VARIABLEandBIND_ARRAY. In this case, the value bound to a scalar is reused in each iteration.The iteration count for a statement is the minimum number of elements bound by all array bindings. For example, if you bind
:idwith three elements and:namewith two elements at the same time,EXECUTEexecutes only two rounds.EXECUTEreturns the total number of rows affected across all rounds.If the set participating in the binding is empty and the statement contains a
RETURNING INTOclause, the statement is executed once. If the statement does not contain aRETURNING INTOclause, an errorORA-06532is reported.The output placeholder for
RETURNING INTOalso requires binding an empty set of the same type usingBIND_ARRAY. AfterEXECUTEcompletes, you can retrieve all returned rows usingVARIABLE_VALUE. The number of returned rows may be less than the number of elements in the input set.
Examples
Create the test table t_bind_array_demo.
CREATE TABLE t_bind_array_demo (id NUMBER, name VARCHAR2(20));
Example 1: Use Number_Table and Varchar2_Table to batch insert three rows of data. The EXECUTE statement returns the total number of rows affected.
DECLARE
c INTEGER;
v_ids DBMS_SQL.NUMBER_TABLE;
v_names DBMS_SQL.VARCHAR2_TABLE;
ret INTEGER;
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, 'INSERT INTO t_bind_array_demo(id, name) VALUES(:id, :name)', DBMS_SQL.NATIVE);
v_ids(1) := 1;
v_ids(2) := 2;
v_ids(3) := 3;
v_names(1) := 'a';
v_names(2) := 'b';
v_names(3) := 'c';
DBMS_SQL.BIND_ARRAY(c, ':id', v_ids);
DBMS_SQL.BIND_ARRAY(c, ':name', v_names);
ret := DBMS_SQL.EXECUTE(c);
DBMS_OUTPUT.PUT_LINE('affected rows: ' || ret);
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Example 2: Use index1 and index2 to bind only the elements in the collection with indices 2 to 3.
DECLARE
c INTEGER;
v_ids DBMS_SQL.NUMBER_TABLE;
ret INTEGER;
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, 'UPDATE t_bind_array_demo SET name = ''updated'' WHERE id = :id', DBMS_SQL.NATIVE);
v_ids(1) := 1;
v_ids(2) := 2;
v_ids(3) := 3;
v_ids(4) := 4;
DBMS_SQL.BIND_ARRAY(c, ':id', v_ids, 2, 3);
ret := DBMS_SQL.EXECUTE(c);
DBMS_OUTPUT.PUT_LINE('affected rows: ' || ret);
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Example 3: Batch execute DELETE statements with the RETURNING INTO clause, and retrieve the deleted rows using VARIABLE_VALUE.
DECLARE
c INTEGER;
v_ids DBMS_SQL.NUMBER_TABLE;
v_deleted DBMS_SQL.NUMBER_TABLE;
ret INTEGER;
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, 'DELETE FROM t_bind_array_demo WHERE id = :id RETURNING id INTO :out_id', DBMS_SQL.NATIVE);
v_ids(1) := 1;
v_ids(2) := 2;
DBMS_SQL.BIND_ARRAY(c, ':id', v_ids);
DBMS_SQL.BIND_ARRAY(c, ':out_id', v_deleted);
ret := DBMS_SQL.EXECUTE(c);
DBMS_SQL.VARIABLE_VALUE(c, ':out_id', v_deleted);
FOR i IN v_deleted.FIRST .. v_deleted.LAST LOOP
DBMS_OUTPUT.PUT_LINE('deleted id: ' || v_deleted(i));
END LOOP;
DBMS_SQL.CLOSE_CURSOR(c);
END;
/
Query the data in the table to confirm the result of the batch operation.
SELECT * FROM t_bind_array_demo ORDER BY id;
Delete the test table.
DROP TABLE t_bind_array_demo;